Click a specific submit button with JQuery

前端 未结 6 1339
温柔的废话
温柔的废话 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: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:

    
    
    
    
    
    

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

提交回复
热议问题