检查列表框的项目存在使用jQuery [英] Check Listbox items exist using jQuery

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

问题描述

我有2列表框(项目之间移动使用jQuery).Suppose ITEM1已经被选中。然后,如果我选择项目1和ITEM2和ITEM3在一起,只有ITEM2和3应该插在第二个列表框。

我不是从ListBox1中删除的项目。我只需要检查是否选定的项目之一,在Listbox2存在。

  // code
$('#btnAdd')。点击(函数(){    VAR selectedOptions = $('#<%= lstAllRole.ClientID%GT;选项:选择');
    如果(selectedOptions.length == 0){
        警报(请选择选项移动);
        返回false;
    }    如果(selectedOptions.length == 1){
        如果($(#<%= lstSelectedRole.ClientID%GT;选项[值='+ selectedOptions.val()+'])长度的方式> 0){
        }
        其他{
            $('#&下;%= lstSelectedRole.ClientID%GT;')。追加($(selectedOptions).clone());
        }
    }
    否则,如果(selectedOptions.length大于1){//选择多个项目移动 - 仅追加,不能在第二列表框项目       // **我需要在这里验证**    }
});


解决方案

只要运行在foreach功能,你会发现的项目。

// code

 否则如果(selectedOptions.length大于1){    $(selectedOptions)。每个(函数(){
        如果($(#<%= lstSelectedRole.ClientID%GT;选项[值='+ THIS.VALUE +'])长度的方式> 0){            //做你的东西
        }其他{
            $('#&下;%= lstSelectedRole.ClientID%GT;')。追加($(本).clone());
        }
    });
}

I have 2 listboxes (Items moves between them using jquery).Suppose item1 is already selected. Then if I select item1 and item2 and item3 together, only item2 and 3 should inserted in second listbox.

I m not removing item from Listbox1. I need only a checking whether one of the selected items exists in the Listbox2.

//Code
$('#btnAdd').click(function () {

    var selectedOptions = $('#<%=lstAllRole.ClientID %> option:selected');
    if (selectedOptions.length == 0) {
        alert("Please select option to move");
        return false;
    }

    if (selectedOptions.length == 1) {
        if ($("#<%=lstSelectedRole.ClientID %> option[value='" + selectedOptions.val() + "']").length > 0) {
        }
        else {
            $('#<%=lstSelectedRole.ClientID %>').append($(selectedOptions).clone());
        }
    }
    else if (selectedOptions.length > 1) {     // Selecting more than one item to move--only append items which are not in 2nd listbox

       // **I need to validate here**

    }
});

解决方案

Just run a foreach function and you will find the items.

//Code

else if (selectedOptions.length > 1) {

    $(selectedOptions).each(function () {
        if ($("#<%=lstSelectedRole.ClientID %> option[value='" + this.value + "']").length > 0) {

            // Do your stuff
        } else {
            $('#<%=lstSelectedRole.ClientID %>').append($(this).clone());
        }
    });
}

这篇关于检查列表框的项目存在使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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