Javascript HTML 按钮加载提交

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

HTML button onload submit

javascriptbutton

提问by Tabatha M

I have a button example below

我在下面有一个按钮示例

<button id="startrunning">go</button>

There is no form or anything I'm using on the same page java script to detect it's click. It will work like I want it to when I click it. I want to see when the page is loaded it will automatically submit it's self is there a way?

我在同一页面上没有使用任何表单或任何东西来检测它的点击。当我点击它时,它会像我想要的那样工作。我想看看页面加载时它会自动提交它自己有没有办法?

Thank you

谢谢

回答by Jonathan Payne

jsFiddle: http://jsfiddle.net/QEu84/10/

jsFiddle:http: //jsfiddle.net/QEu84/10/

<script type="text/javascript">
    function submit()
    {
        document.getElementById("startrunning").click(); // Simulates button click
        document.submitForm.submit(); // Submits the form without the button
    }
</script>

<body onload="submit()">
    <form id="submitForm">
        <button id="startrunning">go</button>
    </form>
</body>

回答by Koen Peters

You can automatically click it when the page loads, using the code below, but you need a form to submit something (you submit a form, not a button)

你可以在页面加载时自动点击它,使用下面的代码,但你需要一个表单来提交一些东西(你提交的是一个表单,而不是一个按钮)

<body onLoad="document.getElementById('startrunning').click();">