如何判断
<s:select multiple="true" list="acceptors" id = "lst"/>
中某一项是否被选中?
我用
if (document.getElementById("lst").options[i].selected)不行, 是否该方法不行,
求助

解决方案 »

  1.   

    if(document.form1.selectname.selectedIndex==-1)   
            no   selected   
      else   
            already   selected
      

  2.   

    楼主的方法也可以不过比较麻烦,用selectedIndex更简单。
      

  3.   


    var fromList = document.getElementById(idName);
    for (var i = 1; i < fromList.options.length; i ++) {
        if (fromList.options[i].selected) {
            toList.options.add(new Option(fromList.options[i].text, fromList.options[i].value));
        }
    }我这样写问题在哪?
      

  4.   

    document.getElementById("lst").options[i].selectedIndex==-1
    没有选 其他就是选了
      

  5.   

    楼主可以这样试下:
    if(document.getElementById("lst").selectIndex==i)
    其中i为你要判断是否被选中的项,如果被选中,表达式的值会为真,否则为假。
      

  6.   

    if(document.formname.selectname.options[i].selected){...}
      

  7.   


      <tr>
       <td height="25" class="td_gray">&nbsp;接受人:</td>
       <td class="td_white" style="vertical-align: middle" width="110px">
       &nbsp;<s:select cssStyle="width:100px;height:200px" multiple="true" list="acceptors" listKey="key" id="acceptor" listValue="value" name="task.acceptor" onchange="change(this.value)"/>
       </td>
       <td class="td_white" width="100" style="padding-left: 5px;">
       <input type="button" onclick="changeAcceptor('acceptor', 'toList', 'addAll')" value="全部添加 >>"/><br/><br/>
       <input type="button" onclick="changeAcceptor('acceptor', 'toList', 'add')" value="添加 >"/><br/><br/><br/>
       <input type="button" onclick="changeAcceptor('acceptor', 'toList', 'deleteAll')" value="&lt;&lt; 全部删除"/><br/><br/>
       <input type="button" onclick="changeAcceptor('acceptor', 'toList', 'delete')" value="&lt; 删除"/>
       </td>
       <td class="td_white">
       <s:select id="toList" cssStyle="width:100px;height:200px" multiple="true" list="{}" />
       </select>
       </td>
      </tr>function changeAcceptor(fromListId, toListId, type){

    var fromList = document.getElementById(fromListId);

    var toList = document.getElementById(toListId);

    if (type == "addAll") {
    for (var i = 1; i < fromList.options.length; i ++) {
    toList.options.add(new Option(fromList.options[i].text, fromList.options[i].value));
    }
    }
    else if (type == "add") {
    for (var n = 0; n < fromList.options.length; n ++) {
    if (fromList.options[n].select) {
    toList.options.add(new Option(fromList.options[n].text, fromList.options[n].value));
    }
    }
    }
    else if(type == "deleteAll") {
    for (var j = 0; j < toList.options.length; j ++) {
    fromList.options.add(new Option(toList.options[j].text, toList.options[j].value));
    }
    }
    else {
    for (var k = 0; k < toList.options.length; k ++) {
    if (toList.options[k].selected) {
    fromList.options.add(new Option(toList.options[k].text, toList.options[k].value));
    }
    }
    }
    }
    我这样写有问题吗? 问题在哪? 怎么执行不行?
      

  8.   

    还有个问题,为什么<s:select multiple="true" list="acceptors" listKey="key" id="acceptor" listValue="value" name="task.acceptor" onchange="change(this.value)"/>
    在页面中显示出来的只有2条记录,但是遍历的时候总是有3条记录,第一条为空的?
      

  9.   

    var selected = document.getElementsById("lst");
    for(var i=0; i<selected.length;i++){
    if(selected[i].checked == true){
    if(i == ?){
                alert("选中!");
                break;
                    }
    }
    }
    用循环试试...
      

  10.   

    我看源代码的时候是这样
    <select id="acceptor" style="width: 100px; height: 200px;" multiple="multiple">
    <option value="张龙飞">张龙飞</option>
    <option value="姚双喜">姚双喜</option>
    </select>但是document.getElementById("acceptor").options.length是3.
    谁能告诉我是什么原因呢?