from 表单非空验证以及多表单提交

时间:2022-11-06 22:23:44

开发中我们常用到$('#formid').serialize()方法进行表单序列化提交,但也相应催生了表单的非空严重以及多表单提交。

form html:

<form id="formid" action="" method="post">
<table style="width:100%" class="table01" cellspacing="1" cellpadding="5">
<#list customlist as culi>
<tr>
<td class="td_title" width="15%" align="center" nowrap="nowrap">${culi.custom}:</td>
<td align="left" style="word-wrap:break-word;word-break:break-all;" colspan="3">
<input id="${culi.vfg}" name="${culi.vfg}"class="mainall" type="text"
style="width:600px;height:100px" multiline="true" value="${culi.conten}">
</td>
</tr>
</#list>
</table>
</form>
<form id='formid2'>
<input type="hidden" name="reportid" value="${reportid}"/>
<input type="hidden" name="cerson_id" value="${cerson_id}"/>
<input type="hidden" name="cerson_name" value="${cerson_name}"/>
<input type="hidden" name="dept" value="${dept}"/>
<input type="hidden" name="release_back" value="${release}"/>
<input type="hidden" name="examine_back" value="${examine}"/>
<input type="hidden" name="cnid" value="${cnid}"/>
</form>

               <a class="zxui-linkbutton" iconCls="saveIcon" onclick="save_from('0');">保存</a>

                <sapn id="xfdw_sapn">&nbsp;<a id="xfdw" class="zxui-linkbutton" onclick="save_from('1');" iconCls="downloadIcon">发送督查室</a>&nbsp;</sapn>

js:

function save_from(saveflag){
//设定一个确认值
var sure = 1;
$('#formid input').each(function(){
var cnt = $.trim($(this).val());
if(cnt =='' || cnt == null){
//当表单中出现空值我们修改确认值状态为“0”
sure = 0;
//节约性能跳出each
return false;
}
});
//我们只有在提交到上级单位时候会去判表单是否存在空值,而仅仅是保存时还是要执行保存动作的
if (sure || saveflag == 0) {
//多表单提交拼接用‘&’
var daall = $('#formid').serialize() +'&'+$('#formid2').serialize();
$.ajax({
type:'post',
url:'${ctx}/secondPhase/in_reoprt_content.pt?release='+saveflag,
data:daall,
success:function(data){
var obj=eval('('+data+')');
var rebackName=obj.datamap.rebackName;

if ('saveSuccess'== rebackName ) {
$.messager.alert('提示','保存成功!');
} else if('saveFail' == rebackName){
$.messager.alert('提示','保存失败!');
} else{
$.messager.confirm('提示:','上报**室'+rebackName+'成功!',function(r){
window.history.go(-1);
});
}
}
});
} else {
$.messager.alert('提示:','请完整填写上报内容!');

}
}