声明
需要jq
还需要我的其他小工具
* in_array
调用
function mustFinish_init(){
var mustFinish_list=[
"username",
"password",
]
MF=new mustFinish();
//简易用法
//MF.init("label",mustFinish_list);
//完整用法
MF.labels=$(".clearfix > div > label");
MF.list=mustFinish_list;
MF.init();
}
mustFinish_init();
源码
/*===========================================必填选项*/
function mustFinish(){
var that=this;
this.labels=$("label");//需要标*的地方
this.list=[];//name数组
this.init=function(labels,list){
that.labels=(labels)?$(labels)||that.labels:that.labels;
that.list=list||that.list;
//必填选项标红
that.labels.map(function(index,element){
var namels=$(element).parent().find("[name]");
var isInList=false;
namels.map(function(ind,input){
var namevalue=input.name;
if(in_array(namevalue,that.list)){
isInList=true;
}
})
if(isInList){
var span=createElement("span","","*");
span.style.color="red";
$(element).prepend(span);
}
})
}
this.check=function(){
var isPass=true;
that.list.map(function(ele,ind){
// console.dir($("[name='"+ele+"']"))
var obj=$("[name='"+ele+"']");
var inputValue="";
var Nodename=obj[0].nodeName;
var inputType;
if(Nodename=="SELECT"){inputType="select";}
else{inputType=obj.attr("type");}
switch (inputType) {
case "text":
obj=$(obj[0]);
inputValue=obj.val();
break;
case "password":
obj=$(obj[0]);
inputValue=obj.val();
break;
case "radio":
inputValue=obj.filter(":checked").val();
break;
case "checkbox":
inputValue=obj.filter(":checked").map(function(c_ind,c_ele){return c_ele.value});
break;
case "select":
// console.dir(obj);
var selecter=obj[0];
inputValue=selecter.selectedOptions[0].value;
inputValue=(inputValue=="请选择")?"":inputValue;
break;
default:
obj=$(obj[0]);
inputValue=obj.val();
}
//console.log(inputValue);
if(!inputValue||inputValue==""){
isPass=false;
}
})
return isPass;
}
}