在Javascript中单击()一个名为“submit”的按钮 [英] click() a button named 'submit' in Javascript

查看:80
本文介绍了在Javascript中单击()一个名为“submit”的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML:

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

我想点击()它。我无法改变HTML方面的任何内容。当我做getElementById(提交)。点击(),我得到这个:

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

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

任何提示?

推荐答案

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

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:

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();
}

这篇关于在Javascript中单击()一个名为“submit”的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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