0

当我调用该 LoadDropDown 函数时,我需要选择多个值。但它不起作用....请建议如何选择多个选项。

<td>Select Profession:</td>
<td>
<select name="selectcontent_profession" id="selectcontent_profession" multiple>
<option value="0">None</option>
<option value="10">Student</option>
<option value="201">Lecturer</option>
<option value="333">Head Of Department</option>
<option value="421">Principal</option>
<option value="523">Chairman</option>
<option value="667">Management</option>
<option value="784">Placement Officer</option>
</select>
</td>

<%  
String s1= "10,333,421,523";
String[]  array = s1.split(",");
%>

<script >
function LoadDropDown()
{
<%for(int i=0;i<array.length;i++){%>

document.getElementById("selectcontent_profession").value ="<%= array[i]%>";

<%}%>
}
</script>
4

3 回答 3

0

首先,您必须声明 select 的多选选项,如下所示:

<select name="selectcontent_profession" id="selectcontent_profession" multiple="multiple"> 

你的功能应该是这样的:

var fld = document.getElementById('selectcontent_profession');
for(var i=0;i<fld.options.length;i++){
for(var j=0;j<array1.length;j++){
    if(fld.options[i].value==array1[j])fld.options[i].selected=true;
    }
}

您正在尝试获取下拉列表的值,而您必须将其设置为选中。

于 2013-07-15T07:20:08.770 回答
0
document.getElementById("selectcontent_profession").options[<%=array[i]%>].defaultSelected =true;

在循环中使用此语句。

于 2013-07-15T06:46:50.197 回答
0

你必须add选择像

var option = document.createElement("option");
option.text = "<%= array[i]%>";
option.value = "<%= array[i]%>";
 document.getElementById("selectcontent_profession").options
                                                       .add(option);
于 2013-07-15T06:35:46.053 回答