在javascript中获取列表框的选定项目

Eri*_*ric 3 javascript vb.net asp.net

我在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++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

TSt*_*per 5

尝试:

 //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));
  }                  
Run Code Online (Sandbox Code Playgroud)