Click a specific submit button with JQuery

前端 未结 6 1345
温柔的废话
温柔的废话 2021-02-05 03:33

I am clicking a submit button using this:

$(\'input[type=submit]\').click(); 

The problem is that I have more that 1 submit button on my page s

相关标签:
6条回答
  • 2021-02-05 03:36

    Add ids to each button and select the id with jQuery.

    Or, if the forms have ids, then just target the form and submit it like so:

    $("#form-id").submit();
    
    0 讨论(0)
  • 2021-02-05 03:41

    If you add a marker, like a specific id or class to your input, you can make your selector more specific. For example, if you give the button you want the ID of form-btn like this:

    <input type="submit" id="form-btn" />
    

    You can select it like this:

    $('input[type=submit]#form-btn').click();
    

    Or just:

    $('#form-btn').click();
    
    0 讨论(0)
  • 2021-02-05 03:49

    If you know the number of submit inputs and which one (in order) you want to trigger a click on then you can use nth-child() syntax to target it. Or add an ID or a class to each one that separates them from the other.

    Selecting the elements by their index:

    $('input[type="submit"]:nth-child(1)').trigger('click');//selects the first one
    $('input[type="submit"]:nth-child(2)').trigger('click');//selects the second one
    $('input[type="submit"]:nth-child(100)').trigger('click');//selects the 100th one
    

    There are actually several ways to do this including using .eq(): http://api.jquery.com/eq

    Selecting the elements by their id:

    <input type="submit" id="submit_1" />
    <input type="submit" id="submit_2" />
    <input type="submit" id="submit_100" />
    
    <script>
    $('#submit_100').trigger('click');
    </script>
    

    Note that .click() is short for .trigger('click').

    0 讨论(0)
  • 2021-02-05 03:51

    One other suggestion!

    I had a form with multiple submits, that also had varying value attributes, so I couldn't simply submit the form after I performed my built in confirmation.

    Instead, I added a few hidden buttons that I forced a click on after I got a Yes back from my confirmation.

    Here are the buttons:

    <button id="email" name="email" type="submit" class="submit radius confirmation" value="all"><?php echo $this->__('Email Receipts to Selected'); ?></button>
    <button id="email-french" name="email-french" type="submit" class="submit radius confirmation" value="all"><?php echo $this->__('Email French Receipts to Selected'); ?></button>
    <button id="helper-email" name="email" type="submit" class="submit hide" value="all"></button>
    <button id="helper-email-french" name="email-french" type="submit" class="submit hide" value="all"></button>
    

    And here's the jQuery:

    <script type="text/javascript">
    jQuery(function ($) {
        $('[id^=email]').on('click', function (e) {
            e.preventDefault();
    
            var button = $(this);
    
            notify.confirm(<?php echo json_encode($this->__('Are you sure?')); ?>, function (ans) {
                if (ans) {
                    $('#helper-' + button.attr('id')).click();
                }
            });
        });
    });
    </script>
    
    0 讨论(0)
  • 2021-02-05 03:52

    Way late to the party, but if your submit buttons have names:

    $("input[name='submitbuttonname']").click();
    
    0 讨论(0)
  • 2021-02-05 03:53

    hmm it cant work with a wizard form with this intacted

    $("#renqform").validate().settings.ignore = ":disabled";
    return $("#renqform").valid();
    
    0 讨论(0)
提交回复
热议问题