我在asp.net中有两个列表框。在单击按钮时,我想加载一个列表框,其中包含所选项目的元素在另一个框中。问题是这必须在客户端完成,因为当单击按钮时我不允许它提交。我想调用一个javascript函数onselectedindexchange但这是服务器端。有任何想法吗?我应该更清楚吗?
解决方案
enter code here
function Updatelist() {
var sel = document.getElementById('<%=ListBox1.ClientID%>')
var lst2 = document.getElementById('<%=ListBox2.ClientId %>')
var listLength = sel.options.length;
var list2length = lst2.options.length;
for (var i = 0; i < listLength; i++) {
if (sel.options[i].selected) {
//lst2.options.add(sel.options[i].text);
lst2.options[list2length] = new Option(sel.options[i].text);
list2length++;
}
}
}
答案 0 :(得分:4)
尝试:
//onclick for button calls this function
function Updatelist() {
var sel = document.getElementbyId("list1");
var listLength = sel.options.length;
for(var i=0;i<listLength;i++){
if(sel.options[i].selected)
document.getElementById("list2").add(new Option(sel.options[i].value));
}
答案 1 :(得分:1)
更确切地说,我们可以这样做;
function selectedVal(list) {
alert(list.options[list.selectedIndex].text);
}
<select id="listbox" multiple="multiple"
style="height: 300px; width: 200px;"
onclick="javascript:selectedVal(this);">
</select>
答案 2 :(得分:0)
关于如何使用Jquery执行此操作,这是一个很好的article。
您还可以在更新面板中粘贴下拉列表。
答案 3 :(得分:0)
function Updatelist() {
var sel = document.getElementById('<%=ListBox1.ClientID%>')
var lst2 = document.getElementById('<%=ListBox2.ClientId %>')
var listLength = sel.options.length;
var list2length = lst2.options.length;
for (var i = 0; i < listLength; i++) {
if (sel.options[i].selected) {
//lst2.options.add(sel.options[i].text);
lst2.options[list2length] = new Option(sel.options[i].text);
list2length++;
}
}
}