javascript 从jquery调用提交按钮功能?

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

Call submit button function from jquery?

javascriptjquery

提问by James123

I want call OnClick function on page load with out user clicking. Can we do that in jQuery or javascript?

我想在页面加载时调用 OnClick 函数而无需用户点击。我们可以在 jQuery 或 javascript 中做到这一点吗?

     <input name="ctl00$PlaceHolderMain$ButtonSection$RptControls$BtnSubmit" 
        class="ms-ButtonHeightWidth" 
        id="ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit" 
        accessKey="o" onclick="javascript:WebForm_DoPostBackWithOptions(new 
        WebForm_PostBackOptions("ctl00$PlaceHolderMain
                                $ButtonSection$RptControls$BtnSubmit", 
                                "", true, "", "", false, true))" 
        type="button" value="OK"/>

回答by James Allardice

$(document).ready(function() {
    $("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").click();
});

This will trigger the clickevent on the element with the supplied id, and it will run when the document is fully loaded.

这将click在提供的元素上触发事件id,并在文档完全加载时运行。

回答by Amin Eshaq

You can try doing a $("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").trigger('click');This would emulate a click on the button more info here

你可以尝试做一个$("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").trigger('click');这将模拟点击按钮更多信息here

回答by Craig M

You can call trigger and pass the type of event to trigger.

您可以调用触发器并将事件类型传递给触发器。

$('#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit').trigger('click');

回答by JohnFx

You imply in the question title that the button you want to click is a submit button. If so you would be better off calling the submit method of the form instead of the click event of the submit button.

您在问题标题中暗示要单击的按钮是提交按钮。如果是这样,您最好调用表单的提交方法而不是提交按钮的点击事件。

document.forms["myform"].submit()

回答by MikeM

var $btnSubmit = $('#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit');

// Either method will work:

// Method 1: jQuery has a click event pre-defined
$btnSubmit.click();

// Method 2: some events (such as newly defined HTML5 events) 
// may not be pre-defined, so .trigger('[event]') is a way to 
// explicitly invoke that event
$btnSubmit.trigger('click');

回答by Hector Sanchez

Or with javascript inside the body:

或者在正文中使用 javascript:

<body onload="javascript:document.getElementById('ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit').click()" ></body>

The trick is to get the element and call the click method.

诀窍是获取元素并调用 click 方法。

Greetings.

问候。