可以在javascript一个列表框的选定项目 [英] Get a listbox's selected items in javascript

查看:167
本文介绍了可以在javascript一个列表框的选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net两个ListBox。一个按钮的点击我要加载一个列表框与另一个框中选定项的内容。问题是,这必须在客户端完成,因为按钮被点击的时候,不让它就范。我想调用javascript函数onselectedindexchange但是这是服务器端。有任何想法吗?我应该更清楚了吗?

解决方案搜索

 在此输入code
功能Updatelist(){
    VAR SEL =的document.getElementById('<%= ListBox1.ClientID%GT;')
    VAR LST2 =的document.getElementById('<%= ListBox2.ClientId%GT;')
    VAR listLength = sel.options.length;
    VAR list2length = lst2.options.length;
    对于(VAR I = 0; I< listLength;我++){
        如果(sel.options [I] .selected){
            //lst2.options.add(sel.options[i].text);
            lst2.options [list2length] =新的选项(sel.options [I]的.text);
            list2length ++;
        }
    }
}


解决方案

尝试:

  //的onclick的按钮调用该函数
 功能Updatelist(){
 VAR SEL =的document.getElementById(list1的);
 VAR listLength = sel.options.length;
 对于(VAR I = 0; I< listLength;我++){
    如果(sel.options [I] .selected)
    。的document.getElementById(列表2)增加(新选件(sel.options [I]。价值));
  }

I have two listboxes in asp.net. On the click of a button I want to load a list box with the elements of the selected items in the other box. The problem is that this has to be done on the client side because when the button is clicked I don't allow it to submit. I want to call a javascript function onselectedindexchange but that is server side. any ideas? Should i be more clear?

Solution

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

解决方案

Try:

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

这篇关于可以在javascript一个列表框的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆