jQuery Form Submission Parameter Check

孤街浪徒 提交于 2019-12-10 11:55:42

问题


Can I intercept/interrogate the form submission values using jQuery in the $('#formid').submit(function() { }); call? I can't check the value from the page as it's the particular submit button pressed that I need to check. I've found this http://geekswithblogs.net/renso/archive/2009/09/09/intercept-a-form-submit-with-jquery-and-prevent-or-allow.aspx and it works in FF, but not in IE8, so that doesn't help, unfortunately. I also don't want to have to attach submit code to the buttons as I was hoping to make a generic bit of code I could plug into a whole set of forms.

Cheers

MH


回答1:


The latest jQuery 1.4 supports 'live' for submit events now -- meaning you don't have to attach individual handlers to all your forms. A nice example that covers what you've asked is given by Paul Irish here:

http://jquery14.com/day-05/jquery-1-4-hawtness-1-with-paul-irish

Here's my own take:

jQuery(document).ready(function() {

  var pageForms = jQuery('form');

  pageForms.find('input[type="submit"]').live('click', function(event) {
    var submitButton = this;
    var parentForm = jQuery(jQuery(this).parents('form')[0]);
    parentForm.data('submit-button',submitButton);
  });

  pageForms.live('submit', function(event) {

    // Prevent form-submission. You can do this conditionally later, of course
    event.preventDefault();

    // The form that was submitted
    var theForm = jQuery(this);

    // Detect which submit button was pushed
    var submitButton = theForm.data('submit-button');
    console.log('submitButton = ',submitButton.value);

  });

});

HTML:

<form>
  <input type="submit" value="submit form 1" />
</form>

<form>
  <input type="submit" value="submit form 2" />
  <input type="submit" value="submit form 3" />
</form>

http://jsbin.com/equho3/6/edit

EDIT - Sorry, I posted an example here that matched the one in your link! I've now provided a cross-browser solution.



来源:https://stackoverflow.com/questions/2119187/jquery-form-submission-parameter-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!