/** * $.form("#fromid").getjson(); * $.form("#fromid").setjson(obj); * $.form("#formid").clear(); * $.form("#fromid").sleep(); * $.form("#fromid").wakeup(); * $("#id").addoptions(opts,textname,valuename); */ if (jquery) (function($){ jquery.fn.extend( { /** * 得到json对象 */ getjson : function() { //todo 对查询出的结果进行分组 1.有前缀的以第一个前缀为分组 2.没有前缀的各自为一组 var vret = {};//返回对象 var vmap = {}; //用form先过滤掉一些非法的id var vform = this.isform?this:jquery.form(this); vform.each(function(){ if( this.id.match(/.+:.+/) ) {//有前缀 var vprefix = this.id.replace(/:.*$/,"");//取得前缀 (vmap[vprefix]=vmap[vprefix]?vmap[vprefix]:[]).push(this); } else {//没前缀 vmap[this.id]=[this]; }//if }); for( var pro in vmap ) { $.each(vmap[pro],function(i,dfield){ var apath = dfield.id.split(/:/); var vrec = vret; $.each(apath,function(ii,nn){ if( ii == apath.length - 1 ) { //如果是末梢节点,则直接取值 vrec[nn] = _getformfieldvalue(dfield); } else { //如果是中途节点,则赋值 vrec = vrec[nn]?vrec[nn]:(vrec[nn]={}); }//if }); }); }//for return vret; }, /** * 给表单赋值 */ setjson : function(vobj){ if( !vobj ) { return this; }//if var vform = this.isform?this:jquery.form(this); vform.each(function(){ var apath = this.id.split(/:/); var vrec = vobj; var dfield = this; jquery.each(apath,function(i,n){ if(vrec && vrec!=0){ if( i == apath.length - 1 ) { _setformfieldvalue(dfield,vrec[n]); } else { vrec = vrec[n]; if( undefined === vrec ) { return false; }//if }//if } }); }); return this; }, /** * 清空表单 */ clear : function() { var vform = this.isform?this:jquery.form(this); return vform.each(function(){ _setformfieldvalue(this); }); }, /** * 快速清空表单 */ quickclear : function() { this.find('input').val(''); this.find('select').val(''); this.find('textarea').val(''); }, /** * 快速设置表单 */ quicksetjson : function(vobj) { if( !vobj ) { return this; }//if this.find('input,select,textarea,label').each(function(){ if(this.id){ var apath = this.id.split(/:/); var vrec = vobj; var dfield = this; jquery.each(apath,function(i,n){ if(vrec && vrec!=0){ if( i == apath.length - 1 ) { _setformfieldvalue(dfield,vrec[n]); } else { vrec = vrec[n]; if( undefined === vrec ) { return false; }//if }//if } }); } }); }, /** * 休眠 */ sleep : function() { var vform = this.isform?this:jquery.form(this); return vform.each(function(){ _sleeporwakeupformfield(this,true); }); }, /** * 唤醒 */ wakeup : function() { var vform = this.isform?this:jquery.form(this); return vform.each(function(){ _sleeporwakeupformfield(this,false); }); }, /** * 表单 */ form : function() { return jquery.form(this); }, addoptions : function(opts,text,value) { if(opts!=null&&opts.length>0){ for(var i=0; i"+opts[i][text]+"").attr(opts[i]).appendto(this); } } return this; } });//jquery.fn.extend /** * 休眠或唤醒单个元素 * @param dfield * @param vflag * @return */ function _sleeporwakeupformfield(dfield,vflag) { switch(dfield.tagname.touppercase()) { case "input": switch( dfield.type.touppercase() ) { case "radio": jquery(":radio[name='"+dfield.name+"']").attr("disabled",vflag); break; case "checkbox": jquery(":checkbox[name='"+dfield.name+"']").attr("disabled",vflag); break; default: jquery(dfield).attr("disabled",vflag); break; }//switch break; case "textarea": jquery(dfield).attr("disabled",vflag); break; case "form": break; case "div": break; case "table": break; default: jquery(dfield).attr("disabled",vflag); break; }//switch }//_sleeporwakeupformfield /** * 从表单区域中取值 * @param dfield * @return */ function _getformfieldvalue(dfield) { var value = undefined; switch(dfield.tagname.touppercase()) { case "button": break; case "label": value = jquery(dfield).text(); break; case "span" : value = jquery(dfield).html(); break; case "input": switch( dfield.type.touppercase() ) { case "button": break; case "radio": $(":radio[name='"+dfield.name+"']:checked").each(function(){ value = this.value; return false; }); break; case "checkbox": var avalues = []; jquery(":checkbox[name='"+dfield.name+"']:checked").each(function(){ avalues.push(this.value); }); value = avalues.length?avalues:value; break; default: value = $(dfield).val(); break; }//switch break; case "textarea": value = jquery(dfield).val(); break; default ://默认情况下是对value属性进行赋值 value = jquery(dfield).val(); break; }//switch //-----------------这里执行转换--------------------- //-对日期的转换 var format = dfield.getattribute('dateformat'); if( format && typeof(value) === "string" ) { if (!value){ return null; } //如果当前field有dateformat属性,则表明他企图是一个日期型 value = date.parsedate(value,format); }//if //-对整数的转换 if( dfield.ratio && typeof(value) === "string" ) { if (!value){ return null; } //如果当前field有ratio属性,则表明他企图是一个integer value = parseint(value)*parseint(dfield.ratio); }//if //------------------------------------------------ return value; }//_getformfieldvalue function _setformfieldvalue(dfield, value) { var isclear = undefined === value; value = (value!=0 && !value)?"":value; //-----------------这里执行转换--------------------- //-对日期的转换 if( !isclear && value && typeof(value) === "object" && value instanceof date ) { //如果当前field有dateformat属性,则表明他企图是一个日期型 var format = dfield.getattribute('dateformat'); var vformat = format?format:"y-m-d h:i:s"; value = value.dateformat(vformat); }//if //-对整数的转换 if( !isclear && value && typeof(value) === "number" && dfield.ratio ) { //如果当前field有ratio属性,则表明他企图是一个integer value = value/parseint(dfield.ratio); }//if //------------------------------------------------ switch(dfield.tagname.touppercase()) { case "button": break; case "label": jquery(dfield).text(value); break; case "span" : jquery(dfield).html(value); break; case "select": if( isclear ) { jquery(dfield).find("option:first").attr("selected",true); } else { jquery(dfield).val(value); }//if break; case "input": switch( dfield.type.touppercase() ) { case "button": break; case "radio": jquery(":radio[name='"+dfield.name+"']").each(function(){ jquery(this).attr("checked",false); if(!isclear && this.value == value) { jquery(this).attr("checked",true); }//if }); break; case "checkbox": $(":checkbox[name='"+dfield.name+"']").each(function(){ $(this).attr("checked",false); if( !isclear ) { value = value instanceof array?value:[""+value];//弄成一个数组 for( var i=0; i= 0) { jquery(this).attr("checked",true); }//if }//if }); break; default: value = jquery(dfield).val(value); break; }//switch break; case "textarea": value = jquery(dfield).val(value); break; default ://默认情况下是对value属性进行赋值 value = {}; break; }//switch }//_setformfieldvalue jquery.extend( { /** * $.form 找出所有注册的子元素 */ form : function(vselector) { var aselect = null; jquery(vselector).each(function(){ if(aselect==null){ aselect = jquery("[id^='"+this.id+":'],[id='"+this.id+"']"); }else{ //aselect= jquery(aselect.selector+','+"[id^='"+this.id+":'],[id='"+this.id+"']"); aselect = jquery([aselect,jquery("[id^='"+this.id+":'],[id='"+this.id+"']")]); } }); aselect.isform = true; return aselect; } });//jquery.extend })(jquery);