检测单击了哪个提交按钮

检测单击了哪个提交按钮

本文介绍了检测单击了哪个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题听起来很像其他问题,但是我发誓我在任何地方都找不到合适的解决方案.我有一个具有多个提交按钮的旧式表单.如果单击,则需要做一些客户端验证,并可能停止提交.如果单击另一个,则不需要进行此验证.

I know this question sounds a lot like a bunch of others that are out there, but I swear I can't find the right solution anywhere. I have a legacy form that has multiple submit buttons. If one is clicked, I need to do a bit of client-side validation and potentially stop the submit. If the other is clicked, I don't need to do this validation.

我发现的是,如果创建一个.submit()处理程序,我似乎无法访问实际单击了哪个按钮.另一方面,如果捕获了我需要担心的按钮的.click()事件,那么我不能阻止表单通过.preventDefault()(或.stopImmediatePropagation())提交.

What I'm finding is that if I create a .submit() handler, I can't seem to access which button was actually clicked. On the other hand, if I capture the .click() event of the button I need to worry about, then I can't prevent the form from submitting via .preventDefault() (or .stopImmediatePropagation()).

这是尝试使用按钮.click()处理程序的代码的最新迭代:

Here's the most recent iteration of the code that attempts to use the buttons .click() handler:

$('#nextButton').click( function( e ) {
  e.preventDefault(); // The form submits anyway (which kind of makes sense)
  // return false also doesn't prevent the submission

  // If any session question is unanswered, abort
  /* $('#registrants input:text[id$="Answer"]').each( function( i, input ) {
    if( $.trim( $(input).val() ) == '' ) {
      submit = false;
    }
  });*/
});

我在这里想念什么?有一种方法可以做到这一点,而且看起来应该很简单,但是如果我有运气的话,我该死的.

What am I missing here? There has to be a way to do this and it seems like it should be fairly simple, but I'll be damned if I've had any luck at all.

谢谢.

推荐答案

也许有帮助: http://geekswithblogs.net/renso/archive/2009/09/09/intercept-a-form-submit-with-jquery-and-prevent-or-allow.aspx

基本上说的是在表单commit()处理程序中检查按钮ID.

Basically what it says is to check the button ID in the form submit() handler.

http://jsfiddle.net/4wnyY/5/

这篇关于检测单击了哪个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:28