使用JQuery单击特定的提交按钮 [英] Click a specific submit button with JQuery

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

问题描述

我正在使用以下方法单击提交"按钮:

I am clicking a submit button using this:

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

问题是我的页面上有1个以上的提交按钮,因此我需要定位一个特定的提交按钮.

The problem is that I have more that 1 submit button on my page so I need to target a specific submit button.

我该怎么办?

推荐答案

如果知道submit输入的数量以及想要触发click的哪个(按顺序),则可以使用nth-child()以它为目标的语法.或为每个ID添加一个ID或一个类,以将它们彼此隔离.

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.

通过索引选择元素:

$('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

实际上有几种方法可以做到这一点,包括使用.eq(): http://api.jquery.com/eq

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

通过其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>

请注意,.click().trigger('click')的缩写.

这篇关于使用JQuery单击特定的提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆