How to find the submit button in a specific form in jQuery

后端 未结 7 2323
执念已碎
执念已碎 2020-12-25 09:37

I try to get jQuery object of a submit button in a specific form (there are several forms on the same page).

I managed to get the form element itself. It looks somet

相关标签:
7条回答
  • 2020-12-25 10:19

    In case you want to find the submit button of the form after it was submitted, you may find the following useful ... I use it to disable the submit button after the form was submitted to prevent multiple clicks.

    $("form").submit(function () {
      if ($(this).valid()) { // in case you have some validation
        $(this).find(":submit").prop('disabled', true);
        $("*").css("cursor", "wait"); // in case you want to show a waiting cursor after submit
      }
    });
    
    0 讨论(0)
  • 2020-12-25 10:21

    BTW: Last selector looks weird. It selects each curSubmit(hm?) in every input[type=submit] tag. May be you mean var curSubmit = $("input[type=submit]", curForm);

    0 讨论(0)
  • 2020-12-25 10:23

    The following should work:

    var submit = curElement.closest('form').find(':submit');
    
    0 讨论(0)
  • 2020-12-25 10:28

    This should work:

    var curSubmit = $("input[type=submit]",curForm);
    

    EDIT: Note the missing ' in the selector

    0 讨论(0)
  • 2020-12-25 10:29

    Because a HTML5 submit button may be out of form tag http://www.w3.org/TR/html-markup/input.submit.html#input.submit.attrs.form, you can use the following code to find it:

    $(curElement.closest('form').get(0).elements).filter(':submit')
    
    0 讨论(0)
  • 2020-12-25 10:32

    Using plain javascript (without relying on jquery):

    var curSubmit = curForm.querySelector('button[type="submit"]');
    
    0 讨论(0)
提交回复
热议问题