繁体   English   中英

如何通过提交按钮以外的其他方式提交表单时,如何触发事件?

[英]How do I fire an event when a form is submitted by something other than the submit button?

请考虑以下代码:

HTML

<a href='#' id='the-button'>Submit</a>

JavaScript的

$("#the-button").click(function(e) {
  e.preventDefault();
  $("#the-form").submit();
});

$("#the-form").submit(function(e) {
  //stuff here
});

单击“#the-button”并提交表单时,“submit”事件处理程序不会触发。 有没有办法将事件处理程序附加到窗体,以便即使它不是通过提交按钮提交它也会触发?

更改$("#the-form").submit(); to $("#the-form").trigger('submit');

编辑:但是,就像@wirey说的那样,你发布的代码应该有效,除非有其他代码干扰。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM