使用 JQuery 单击特定的提交按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8319688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:21:09  来源:igfitidea点击:

Click a specific submit button with JQuery

jqueryjquery-selectors

提问by Satch3000

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 so I need to target a specific submit button.

问题是我的页面上有超过 1 个提交按钮,所以我需要定位一个特定的提交按钮。

How could I do that?

我怎么能那样做?

回答by Jasper

If you know the number of submitinputs and which one (in order) you want to trigger a clickon 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.

如果您知道submit输入的数量以及要触发的输入数量(按顺序)click,那么您可以使用nth-child()语法来定位它。或者为每一个添加一个 ID 或一个类,将它们与另一个分开。

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

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

Selecting the elements by their id:

通过 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').

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

回答by Jacob

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-btnlike this:

如果您添加一个标记,例如特定的 id 或类到您的input,您可以使您的选择器更加具体。例如,如果你给按钮提供你想要的 ID,form-btn如下所示:

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

You can select it like this:

您可以像这样选择它:

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

Or just:

要不就:

$('#form-btn').click();

回答by Purag

Add ids to each button and select the idwith jQuery.

ids添加到每个按钮并id使用 jQuery选择。

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

或者,如果表单有ids,那么只需定位表单并像这样提交它:

$("#form-id").submit();

回答by Grim...

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

聚会迟到了,但如果您的提交按钮有名称:

$("input[name='submitbuttonname']").click();

回答by Tyler Swartzenburg

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:

这是 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>

回答by Eugene Tang

hmm it cant work with a wizard form with this intacted

嗯,它不能与完好无损的向导表单一起使用

$("#renqform").validate().settings.ignore = ":disabled";
return $("#renqform").valid();