jquery - 使用 jQuery 检查列表框项目是否存在

标签 jquery asp.net

我有 2 个列表框(项目使用 jquery 在它们之间移动)。假设 item1 已被选中。然后,如果我同时选择 item1、item2 和 item3,则只有 item2 和 3 应该插入到第二个列表框中。

我不会从 Listbox1 中删除项目。我只需要检查 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**

    }
});

最佳答案

假设这就是你想要的..

试试这个

else if (selectedOptions.length > 1) {     // Selecting more than one item to move--only append items which are not in 2nd listbox

    var tempArray= $.map(selectOptions, function(n){
          return this.value;
    });

    if($.inArray("valueTochekcInSelectedOption", tempArray) != -1) 
    {
        //value not selected in list 1
    }else{
       //value selected in list 1
    }
}

关于jquery - 使用 jQuery 检查列表框项目是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15516781/

相关文章:

javascript - 如何在不使用 [] 的情况下在 IE8 上的 javascript 中选择对象的第一个元素?

javascript - 显示/隐藏 SQL 数据库中的动态 ID

asp.net - @model 到底做了什么?

c# - 为什么使用 ASP.NET 验证器进行验证?

javascript - 使用 css3/jquery 来回“切换”元素的旋转

jquery - jQuery.ajax() 成功执行之前调用的其他函数

android - 支持 native 移动应用程序中的 ASP.Net Forms 身份验证

javascript - 上传视频时出现内部服务器错误。但上传图片工作正常。可能会出现什么问题?

asp.net - SharePoint - 使用 ASP 获取登录用户,使用 javascript 进行操作?

javascript - 如何让 3 个 div 在容器内水平对齐,高度为 100%