form表单数据处理插件

xiaoxiao2021-02-28  19

/** * @description :form处理插件,此插件依赖于jquery * */ (function($) { function l(msg){ console.log(msg); }; $.extend($.fn, { loadForm : function(options){ var $this = $(this); var formParams = { }; $.extend(formParams, options); return this.each(function(){ var data = formParams.data||{}; for(var key in data){ $this.find("[name='"+key+"']").val(data[key]); } }); }, reset : function(){ var $this = $(this); var $reset = $this.find("input[type='reset']"); if($reset.length == 0){ $reset = $("<input type='reset' style='display:none;'/>"); $this.append($reset); } return this.each(function(){ $reset.trigger("click"); }); }, handleForm: function(options) { var $this = $(this); var formParams = { }; $.extend(formParams, options); return this.each(function(){ $this.find("[isTiggerSubmit]").keydown(function(event){ if(event.keyCode == 13){ //绑定回车 formParams.$submitBtn.trigger("click"); } }); formParams.$submitBtn.unbind("click").click(function(){ var $pageTable = formParams.$pageTable; if($pageTable){ var pageParam = $pageTable.getPageParam($pageTable); pageParam.page = 1; var formData = (formParams.getExpandFormData&&formParams.getExpandFormData($this))||$this.getFormData(); pageParam.queryParams = formData; $pageTable.showPage(pageParam); } return false; }); }); }, getFormData : function(options) { var $this = $(this); var formParams = { }; $.extend(formParams, options); var arr = $this.serializeArray(); var resultObj = {}; $.each(arr,function(){ resultObj[this.name] = this.value; }); formParams.expandFormData && $.each(formParams.expandFormData,function(){ resultObj[this.name] = this.value($this); }); return resultObj; } }); })(jQuery);

example:

$("#formId").getFormData({ expandFormData:[ { name:"isAll", value:function (){ return 0; } } ]} )
转载请注明原文地址: https://www.6miu.com/read-2400144.html

最新回复(0)