从列表框中选择多个值 [英] select multiple values from listbox

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

问题描述

我有一个像这样的列表框

I have a list box like this

<select id="ddlBranch" name="ddlBranch" multiple runat="server" style="width:100%;">
</select>


在此列表框中没有任何项目

在文本框模糊的情况下,我正在从数据库中获取一些值
我需要检索与列表框匹配的任何项目,都应在列表框中全部选择

在以下脚本的帮助下,我只能从列表框中选择一个值


in this listbox there are no of items

on blur of textbox i am fetching some values from database
I need whatever items i am retrieving matching with listbox should be all selected in list box

with the help of following script i am getting able to selected only one value from listbox

function getSelet()
           {
                 if (document.getElementById('hidBranchLst').value != '') {
                var strBranch = document.getElementById('hidBranchLst').value
                var strBranchList = strBranch.split('|');
                var MulSel = document.getElementById('ddlBranch');
                MulSel.multiple = true;
                for (var j = 0; j < strBranchList.length; j++) {
                    alert(strBranchList[j].toString());
                    for (var i = MulSel.options.length - 1; i >= 0; i--) {
                        if (MulSel.options[i].value == strBranchList[j].toString()) {
                            MulSel.selectedIndex = i;
                        }
                    }
                }
            }
        }

推荐答案

请查看以下线程:

http://www.webdeveloper.com/forum/showthread.php?t=34494 [ ^ ]

当前,您正在设置列表框的selectedIndex,但这只会选择一个.您需要将每个选项设置为选中状态,如上面的线程所示.代替MulSel.selectedIndex = i;,您应该尝试MulSel.options[i].selected = true;.

希望这会有所帮助,

埃德:)
Please have a look at this thread:

http://www.webdeveloper.com/forum/showthread.php?t=34494[^]

Currently you are setting the selectedIndex of the list box but that will only select one. You need to set each option as being selected, as shown in the above thread. Instead of MulSel.selectedIndex = i; you should try MulSel.options[i].selected = true;.

Hope this helps,

Ed :)


这篇关于从列表框中选择多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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