click() Javascript 中名为“提交”的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4571497/
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-10-25 14:08:32  来源:igfitidea点击:

click() a button named 'submit' in Javascript

javascriptjavascript-objectsdom-events

提问by Karlo

I have this HTML:

我有这个 HTML:

<input type="submit" name="submit" value="Invoeren" accesskey="s" class="buttons"/>

I'd like to click() it. I can not change anything on the HTML-side. When I do getElementById("submit").click(), I get this:

我想点击()它。我无法更改 HTML 端的任何内容。当我这样做时getElementById("submit").click(),我得到了这个:

>>> document.getElementById("submit").click();
Cannot convert 'document.getElementById("submit")' to object

Any hints?

任何提示?

回答by mplungjan

NOTE: NEVER call anything in a form "submit"! It will hide the form's submit event from script.

注意:切勿以“提交”形式调用任何内容!它将从脚本中隐藏表单的提交事件。

You have many ways of accessing the button

您有多种访问按钮的方法

document.querySelector("[name=submit]").click(); // first named button

document.querySelector("#form [name=submit]").click(); // in form with id="form"

Old IEs would shadow the name so you could use getElementById on a name but it is not recommended.

旧 IE 会隐藏名称,因此您可以在名称上使用 getElementById,但不建议这样做。

Older methods:

较旧的方法:

document.getElementsByName("submit")[0].click();

where 0 is the first element on the page named submit.

其中 0 是名为 submit 的页面上的第一个元素。

document.forms[0].elements[0].click(); 

where forms[0]is the first form and elements[0]is the first element

其中forms[0]是第一个形式,elements[0]是第一个元素

or

或者

document.getElementsByTagName("form")[0].elements[0].click(); 

where ("form")[0]is the first form on the page and elements[0]is the first element

哪里("form")[0]是页面上的第一个表单,elements[0]是第一个元素

回答by Alex Vidal

Since you can't edit the actual HTML (to add the id attribute), and you want this to be cross-browser, you could loop over all of the input elements and check the type and value attributes until it matches your submit button:

由于您无法编辑实际的 HTML(添加 id 属性),并且您希望它是跨浏览器的,您可以遍历所有输入元素并检查 type 和 value 属性,直到它与您的提交按钮匹配:

function get_submit_button() {
    var inputs = document.getElementsByTagName('INPUT');
    for(var i=0; i < inputs.length; i++) {
        var inp = inputs[i];
        if(inp.type != 'submit') continue;
        if(inp.value == 'Invoeren' && inp.name == 'submit') {
            return inp;
            break; // exits the loop
        }
    }
    return false;
}

function click_submit() {
    var inp = get_submit_button();
    if(inp) inp.click();
}

回答by Phrogz

You are using getElementByIdbut you don't have an idattribute on your input. Just because IE totally fubars the namespace and treats nameattributes somewhat like idattributes does not mean that you should.

您正在使用,getElementById但您id的输入没有属性。仅仅因为 IE 完全破坏了命名空间并将name属性视为id属性,并不意味着您应该这样做。

<input id="submit" ... />
...
document.getElementById('submit').click();

回答by Sarfraz

The immediate issue i can see is that you have NOT assigned id submitto your input, so this won't work:

我可以看到的直接问题是您没有submit为您的输入分配 id ,所以这将不起作用:

document.getElementById("submit").........

unless you specify the id as well:

除非您也指定了 id:

<input id="submit" type="submit" name="submit" value="Invoeren" accesskey="s" class="buttons"/>

回答by mrtsherman

Your input has no id attribute so it can't be retrieved by id. Use this instead.

您的输入没有 id 属性,因此无法通过 id 检索。改用这个。

<input id="submit" type="submit" name="submit" value="Invoeren" accesskey="s" class="buttons"/>

回答by Malvolio

There is no element with the ID "submit" -- you have an element named"submit". As Dylan points out, jQuery would make everything easier, but you can fix it yourself. If you cannot change the HTML, do something like this:

没有 ID 为“submit”的元素——您有一个名为“submit”的元素。正如 Dylan 指出的那样,jQuery 会让一切变得更简单,但您可以自己修复它。如果您无法更改 HTML,请执行以下操作:

var buttons = document.getElementsByTagName("submit");
for (var i=0; i < buttons.length; i++) {
  var button = buttons[i];
  if (button.getAttribute("name") === "submit") {
     button.onclick = whatever ...

回答by Dylan

You might want to look into jQuery. It makes everything so, so much easier.

您可能想研究 jQuery。它让一切变得如此、如此简单。