$('#remove').click(function() {
var foo = [];
$('#FeatureLists :selected').each(function(i, selected) {
foo[i] = $(selected).text();
alert(foo[i]);
if (foo[i] != "Add" )
return !$('#FeatureLists option:selected').remove();
if (foo[i] != "Edit")
return !$('#FeatureLists option:selected').remove();
});
});
我的选择中有六个项目,其中4个是添加,编辑,删除视图,它是多选列表,我不希望用户删除这4个项目,除此之外他们可以删除任何项目。我该怎么做?它不会发生在上面的代码中
答案 0 :(得分:2)
首先,根据您的示例代码,似乎不需要数组foo
。其次,if语句需要包含需要用OR
条件排除的所有项目,如下所示:
if (foo[i] == "Add" ||
foo[i] == "Edit" ||
foo[i] == "Delete" ||
foo[i] == "View")
{
return;
}
else
{
$(selected).remove();
}
答案 1 :(得分:1)
试试这个。简单的一行代码和稍快的
$(document).ready(function() {
$("input").click(function(event) {
var foo = [];
$('#FeatureLists :selected').each(function(i, selected) {
foo[i] = $(selected).text();
if (foo[i] != "Add" && foo[i] != "Edit" && foo[i] != "Delete" && foo[i] != "View") return $(selected).remove();
});
});
});