我点击使用此按钮:
$('input[type=submit]').click();
问题是我的页面上有1个以上的提交按钮,因此我需要定位一个特定的提交按钮。
我该怎么办?
最佳答案
如果您知道submit
输入的数量以及要触发click
的数量(顺序),则可以使用nth-child()
语法来定位它。或为每个ID或一个ID添加一个类,以将它们彼此分开。
通过索引选择元素:
$('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通过其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')
的缩写。