【问题标题】:Check Listbox items exist using jQuery使用 jQuery 检查列表框项目是否存在
【发布时间】:2013-03-20 14:51:01
【问题描述】:

我有 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**

    }
});

【问题讨论】:

    标签: jquery asp.net


    【解决方案1】:

    假设这是你想要的..

    试试这个

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

    【讨论】:

      【解决方案2】:

      只需运行一个 foreach 函数,您就会找到这些项目。

      //代码

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

      【讨论】:

        【解决方案3】:

        我认为您无需区别对待仅选择一个或多个选项的情况。只需获取选定元素的列表并遍历它,如果它不存在,则添加到第二个列表中。类似的东西应该可以工作:

        $('#btnAdd').click(function () {
        
        var selectedOptions = $('#<%=lstAllRole.ClientID %> option:selected');
        
        var optionsAlreadyInTheList = $('#<%=lstSelectedRole.ClientID %> option');
        
        if (selectedOptions.length == 0) {
            alert("Please select option to move");
            return false;
        } else {
            selectedOptions.each(function() {
                isInTheList = false;
                for(i=0; i< optionsAlreadyInTheList.length; i++) {
                    if ($(this).val() == optionsAlreadyInTheList[i].val()) {
                        isInTheList = true;
                    }
                }
                if( ! isInTheList ) {
                    $('#<%=lstSelectedRole.ClientID %>').append($(selectedOptions).clone());
                }
            });        
        
        }
        });
        

        这段代码只是为了给出一个想法。我在没有测试的情况下编写了它,并且可能无法按原样工作。

        【讨论】:

          猜你喜欢
          • 2021-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多