如何通过单击复选框来触发表单提交?
我正在使用 jQuery 1.6,我想通过单击复选框按钮来提交表单。
也就是说,我有以下提交表单的代码
$jQuery('#search_form').submit(function () {
$jQuery.get(this.action, $jQuery(this).serialize(), null, 'script');
return false;
});
,当我单击 HTML 代码所在的复选框时,
<input type="checkbox" value="true" ... id="check_box_id">
我想触发表单提交。
我该怎么做?
I am using jQuery 1.6 and I would like to submit a form by clicking on a check box button.
That is, I have the following code that submits a form
$jQuery('#search_form').submit(function () {
$jQuery.get(this.action, $jQuery(this).serialize(), null, 'script');
return false;
});
and when I click on a check box which HTML code is
<input type="checkbox" value="true" ... id="check_box_id">
I would like to trigger the form submission.
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过简单地调用不带参数的
submit
来触发submit
事件:或者,您可以使用
trigger
方法:因为看起来您在提交事件处理程序中触发异步请求,而不是使用默认的表单行为,所以我建议禁用该复选框(或删除事件处理程序)
submit
事件处理程序,然后在get
方法的成功回调中重新启用它,以防止用户重复提交。You can trigger the
submit
event by simply callingsubmit
with no arguments:Alternatively, you can use the
trigger
method:As it looks like you're firing an asynchronous request in the submit event handler, rather than using the default form behaviour, I would suggest disabling the checkbox (or removing the event handler) in the
submit
event handler, and then re-enabling it in the success callback of theget
method, to prevent the user from submitting repeatedly.在不带参数的情况下调用表单的submit()将触发表单提交。
jQuery 中的大多数事件处理程序都共享这一点;调用 .click() 将模拟单击,而 .click(function...) 设置处理程序。
Calling submit() on the form with no parameters will trigger it to submit.
Most event handlers in jQuery share this; calling .click() will simulate a click, whereas .click(function...) sets a handler.