jQuery表单提交仅触发Account字段校验 未执行PDF字段校验问题求助
问题根因
你写的校验逻辑里,Account字段校验的两个分支都设置了跳出函数的return语句:
- 当Account字段有有效值时,会执行
return true,直接终止当前函数运行,后续PDF的校验逻辑完全不会被触发 - 当Account未填写且带required属性时会执行
return false跳出,同样不会执行后续校验
修复方案
你需要去掉单字段校验通过后的return true语句,只在校验不通过的时候返回false终止流程,等所有必填字段都校验完成后,最后再统一返回true,修改后的校验部分代码如下:
// 校验Account字段 if (hasValue("#Account")) { $("#Account").removeAttr('required'); $("#AccountError").css({ 'display': 'none' }); // 移除原有的return true,不提前跳出函数 } else { if ($("#Account").hasAttr('required')) { $("#Account").focus(); $("#AccountError").css({ 'display': 'block', 'color': 'red' }).html("Please select a valid Account."); return false; } } // 校验PDF字段 if (hasValue("#PDF")) { $("#PDF").removeAttr('required'); $("#PDFError").css({ 'display': 'none' }); // 移除原有的return true,不提前跳出函数 } else { if ($("#PDF").hasAttr('required')) { $("#PDF").focus(); $("#PDFError").css({ 'display': 'block', 'color': 'red' }).html("Please upload only PDF file."); return false; } } // 所有字段校验全部通过后,统一返回true return true;
内容的提问来源于stack exchange,提问作者Notion
相关产品推荐
相关产品推荐

