| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1153 人关注过本帖
标题:如何取<select>的值
只看楼主 加入收藏
sclz_jack
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-11-9
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
如何取<select>的值
现在我有一个表单,里面有两个 <select>,其中为sel1和sel2,需要把sel1里面的内容添加到sel2里面去,最后把sel2里面的值取出来,代码如下:
        <form name="myform" action="save.asp" method="post">
        <table width="240" height="80" border="0" cellpadding="0" cellspacing="0">
        <tr>        
        <td width="40%" align="right">
        <select name="sel_rev1" id="sel_rev1" size="9" multiple>
<%
do while not rs.eof
%>
        <option value=" <%=rs("user")%>"> <%=rs("user")%> </option>
<%
rs.MoveNext
loop
rs.close
set rs=nothing
%>
        </select>
        </td>
        <td width="20%" align="center" valign="middle"> <input name="sure2" type="button" id="sure2" onClick="allsel(document.myform.sel_rev1,document.myform.sel_rev2);" value="增加" align="center" height="2"> <p>
        <input name="sure1" type="button" id="sure1" onClick="allsel(document.myform.sel_rev2,document.myform.sel_rev1);" value="删除">
      </td>
      <td width="40%" align="left"> <select name="sel_rev2" size="9" multiple id="sel_rev2" style="width:100px ">
            </select>
      </td>
      </tr>
    </table>
      </form>
搜索更多相关主题的帖子: select 
2009-11-09 16:42
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:10 
个人一般只用Javascript来取select的值
语法
取出select里选择的值
sel1.options[sel1.selectedIndex].value;
这里面options是一个类似于数组的集合
sel1.selectedIndex默认值为-1
而当某个选项被选中时sel1.selectedIndex就不为-1
其值就是表示第几个选项。因此
sel1.options[sel1.selectedIndex].value;
这句话写在select的onChange="函数()"函数里才能取到值。
2009-11-09 19:09
aspic
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:51
帖 子:2258
专家分:8050
注 册:2008-2-18
收藏
得分:10 
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<script>
var $ = function(id) {
    return typeof id == 'string' ? document.getElementById(id) : id
};
function add(a, b) {
    a = $(a);
    b = $(b);
    var val = a.options[a.selectedIndex].value;
    var text = a.options[a.selectedIndex].text;
    b.options.add(new Option(text, val)); 
};
function del(a) {
    a = $(a);
    if (a.selectedIndex > - 1 ) { 
        a.options[a.selectedIndex] = null ; 
    }else{ 
        alert('请选择要删除的项!')
    } 
}
</script>
<select id="a" style="width:150px;height:100px;float:left" multiple="multiple">
    <option value="1" selected="selected">值1</option>
    <option value="2">值2</option>
    <option value="3">值3</option>
    <option value="4">值4</option>
    <option value="5">值5</option>
</select>
<div style="width:80px;float:left; text-align:center">
    <br />
    <input type="button" onclick="add('a','b')" value="---->" />
    <br />
    <input type="button" onclick="del('b')" value="<----">
</div>
<select id="b" style="width:150px;height:100px;float:left" multiple="multiple"></select>
</body>
</html>
2009-11-10 09:12
sclz_jack
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-11-9
收藏
得分:0 
但是现在如果不选中sel2里的值就获取不到数据,我想要的结果是sel1的值添加到sel2里面提交表单后能够自动选定sel2里面的值,谢谢!!
2009-11-10 16:27
快速回复:如何取<select>的值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015434 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved