2013-07-15 71 views
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> 

回答

0

首先必須聲明的選擇的多重選擇選項,如下所示:

<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; 
    } 
} 

您正試圖獲得下拉,而價值您必須將其設置爲選中狀態。

+0

:如果我使用索引,你的方法將是合適的。但是我想在這裏使用價值。字符串數組包含選項的值。 如果我將選項內的任何值更改爲25. 由於只有7個索引,所以不會選擇該值。 –

+0

你的問題不清楚。你試圖達到什麼目的?爲什麼你會改變你的選擇價值? –

+0

編輯我的答案。但是我仍然覺得你應該檢查你是否會真正改變期權的價值。 –

0

你必須add的選項,如

var option = document.createElement("option"); 
option.text = "<%= array[i]%>"; 
option.value = "<%= array[i]%>"; 
document.getElementById("selectcontent_profession").options 
                 .add(option); 
+0

謝謝,但我的問題是選擇多個值。選項不是問題。 –

0
document.getElementById("selectcontent_profession").options[<%=array[i]%>].defaultSelected =true; 

使用此聲明中循環。

+0

如果我使用索引,你的方法將是合適的。但是我想在這裏使用價值。字符串數組包含選項的值。 如果我將選項內的任何值更改爲25. 由於只有7個索引,所以不會選擇該值。 –