js表单反显

时间:2022-06-21 15:53:31
/*
* 表单自动回显js
* 依赖JQURY
* 使用参考:$("#form1").form("load",{"id":"112","username":"ff","password":"111","type":"admin"}); //id、username、password、type 是 表单 空间name
*/
$(function(){
$.fn.form=function(type,data){
var form=$(this);
for(var i in data){
var name=i;
var value=data[i];
if(name!=""&&value!=""){
valuAtion(name,value);
}
}
function valuAtion(name,value){
if(form.length<1){
return;
}
if(form.find("[name='"+name+"']").length<1){
return;
}
var input=form.find("[name='"+name+"']")[0];
if($.inArray(input.type,["text","password","hidden","select-one","textarea"])>-1){
$(input).val(value);
}else if(input.type==" "||input.type=="checkbox"){
form.find("[name='"+name+"'][value='"+value+"']").attr("checked",true);
}
} };
});