我可以获得多个 .submit() 处理程序的集体结果吗?
我有一个具有多个 .submit 验证的表单。我想注册一个处理程序,如果集体结果为假,它可以执行某些操作。
这是一个非常简单的片段,说明了我想要做的事情...
$("#myForm").submit(function() {
if (conditionA)
{
return true;
}
else {
return false;
}
});
$("#myForm").submit(function() {
if (conditionB)
{
return true;
}
else {
return false;
}
});
// TODO: Have some event here that knows if Collective result of .submit was true or false
$("#myForm").submitParentHandler(function (collectiveResult) {
if (collectiveResult) {
alert("Form will be submitted");
}
else {
alert("Form will NOT be submitted");
}
});
其中最后一个方法目前只是我想要的一个模拟示例。
I have a form that has multiple .submit validations. I would like to register a handler that can do something if the collective result was false.
Here is a very simple snippet that illustrates what I would like to do...
$("#myForm").submit(function() {
if (conditionA)
{
return true;
}
else {
return false;
}
});
$("#myForm").submit(function() {
if (conditionB)
{
return true;
}
else {
return false;
}
});
// TODO: Have some event here that knows if Collective result of .submit was true or false
$("#myForm").submitParentHandler(function (collectiveResult) {
if (collectiveResult) {
alert("Form will be submitted");
}
else {
alert("Form will NOT be submitted");
}
});
Where the last method there is currently just an mocked up example of what I would like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你只需要重新构建解决方案。不要注册多个提交处理程序,而是注册包含验证逻辑的方法,如下所示:
然后您只需注册要调用的验证方法:
当您单击提交时,它将运行
submitHandlers
中的所有方法,并且如果它们都返回 true,你将继续。如果有任何返回 false,您将从提交处理程序返回 false。I think you just need to rearchitect the solution. Instead of registering multiple submit handlers, instead register methods that contain validation logic like so:
Then you would simply register validation methods to invoke:
When you click submit, it would run through all the methods in
submitHandlers
and if they all returned true, you would continue. If any returned false, you would return false from the submit handler.