项目中经常会遇到判断一个元素是否包含在数组中,用Jquery的话很简单,代码如下:
var memberIdArr = new Array();
var win = art.dialog.open.origin;
if("Drgo_RP_assessor"==($("#condition").val())){
win.$("#choice").find("li").each(function(index){
memberIdArr[index] = $(this).attr("id");
});
}else{
win.$("#choiceMaster").find("li").each(function(index){
memberIdArr[index] = $(this).attr("id");
});
}
$(".unchoice_li").each(function(){
var member = "";
if($.inArray($(this).find("input").val(), memberIdArr) != -1){
member = member +"<li class='choice_li' onclick='clickLi(this)'>" + $(this).html() + "</li>";
$(this).remove();
}
$("#choice").append(member);
});
var arr = [ "刘诗诗", "杨幂", "刘亦菲", "周冬雨" ];
$.inArray("周冬雨", arr);
返回4,如果不包含在数组中,则返回 -1。