点击功能有问题
$('#delete').click(function (e) {
e.preventDefault();
var result = true;
if($('.checkbox:checked').length < 1) {
$.notifyBar({
cls: "error",
html: 'At least 1 checkbox must be checked.',
delay: 5000
});
result = false;
}
return result;
});
问题是如果一切顺利,按钮不会提交表单 有没有什么不对的地方?
$('#delete').click(function (e) {
e.preventDefault();
var result = true;
if($('.checkbox:checked').length < 1) {
$.notifyBar({
cls: "error",
html: 'At least 1 checkbox must be checked.',
delay: 5000
});
result = false;
}
return result;
});
the problem is button doesn't submit form if all goes well
Is there any wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对
e.preventDefault()
的调用会阻止提交表单的按钮的默认操作。仅当您不希望发生提交时才需要调用它。The call to
e.preventDefault()
is preventing the default action of the button which is to submit the form. You need to only call that when you don't want a submit to occur.我认为这就是您想要做的:
无论您从函数返回什么,对
e.preventDefault();
的调用都将阻止提交按钮提交。在我的编辑中,只有当结果为 false 时才会阻止默认操作。I think this is what you're trying to do:
The call to
e.preventDefault();
will prevent the submit button from submitting no matter what you return from the function. In my edit the default action is only prevented if result is false.使用
e.preventDefault();
将阻止表单提交,即使返回true
也是如此。Using
e.preventDefault();
will stop the form from submitting, even iftrue
is returned.您应该将其放入 onsubmit 处理程序而不是 click 处理程序中。另外,请确保您希望他们单击的按钮属于提交类型:
You should put this in the onsubmit handler instead of the click handler. Also, ensure the button that you want them to click is of type submit: