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