注册 登录
编程论坛 ASP技术论坛

如何取<select>的值

sclz_jack 发布于 2009-11-09 16:42, 1153 次点击
现在我有一个表单,里面有两个 <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>
3 回复
#2
yms1232009-11-09 19:09
个人一般只用Javascript来取select的值
语法
取出select里选择的值
sel1.options[sel1.selectedIndex].value;
这里面options是一个类似于数组的集合
sel1.selectedIndex默认值为-1
而当某个选项被选中时sel1.selectedIndex就不为-1
其值就是表示第几个选项。因此
sel1.options[sel1.selectedIndex].value;
这句话写在select的onChange="函数()"函数里才能取到值。
#3
aspic2009-11-10 09:12
程序代码:
<!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>
#4
sclz_jack2009-11-10 16:27
但是现在如果不选中sel2里的值就获取不到数据,我想要的结果是sel1的值添加到sel2里面提交表单后能够自动选定sel2里面的值,谢谢!!
1