想用jquery代码实现这样的一个功能:有一个下拉框,当选择下拉框的时候,判断选择的值有没有被选择过,如果有则提示;代码如下:
$(function(){ var authTypes=new Array(); authTypes = $("#authTypes").val().split(','); $("#authType").on("change",function(){ console.log(authTypes) console.log("当前选择的" + $('#authType option:selected').val()) for(var val in authTypes){ console.log(val) if($('#authType option:selected').val() == authTypes[val]){ console.log(val+" selected:" + $('#authType option:selected').val()) alert("该分类已添加过"); return false; } } /* if(checkArray.indexOf($('#authType option:selected').val()) <= -1){ alert("该分类已添加过"); return false; }*/ }) })
大致思路是:在页面放置一个隐藏域,存放已经选择的id,逗号分隔;之后把该值赋值给一个数组;之后对数组循环,如果有则提示;没有则可以选择。