C# 在 jquery 中触发按钮点击的所有可能方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15242483/
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
All possible ways to trigger a button click in jquery
提问by Mathematics
I want to know all possible ways to trigger a button in jQuery, I tried this but it's not working,
我想知道在 jQuery 中触发按钮的所有可能方法,我试过这个,但它不起作用,
$("#<%=btA.ClientID%>").trigger('click');
Note: its a ASP.Net button and what I want is to trigger button click so that it will trigger a code-behind click method of buttonA.
注意:它是一个 ASP.Net 按钮,我想要的是触发按钮单击,以便它触发 buttonA 的代码隐藏单击方法。
采纳答案by K D
Try this
尝试这个
$("#<%=btA.ClientID%>").click();
if it doesn't work try to alert this and check if it is getting accessed by JQ
如果它不起作用,请尝试发出警报并检查它是否被 JQ 访问
alert($("#<%=btA.ClientID%>").length);
Have you registered the event with jquery in following manner
您是否按以下方式使用 jquery 注册了该事件
$(function(){
$("#<%=btA.ClientID%>").click(function(){
// your logic here
});
});
One more thing to confirm, are you loading this button directly on page load or you are having some page update panel which load it afterwords?
还有一件事要确认,你是在页面加载时直接加载这个按钮,还是有一些页面更新面板在后记加载它?
If yes then you should bind the event to button in following manner
如果是,那么您应该按以下方式将事件绑定到按钮
$(document).on('click',"#<%=btA.ClientID%>", function() {...});
回答by Matt Cain
$("#<%=btA.ClientID%>").click();
I believe this is the only other way.
我相信这是唯一的其他方式。
回答by DSharper
$("#<%=btA.ClientID%>").click();
Or
$("input[id$='yourbuttonId'").click();
Reason that trigger not working is Jquery only allow you to trigger a click that Jquery has created. Use the trigger route after you have written a click listener.
触发器不起作用的原因是 Jquery 只允许您触发 Jquery 创建的点击。编写点击侦听器后使用触发器路由。
回答by Prabhu Murthy
May be you are trying to wire the event,when the control itself is not loaded on to the page.
当控件本身未加载到页面时,您可能正在尝试连接事件。
Try this instead.It buys a little bit of time and then wires up the event.
试试这个。它会花一点时间,然后连接事件。
setTimeout(function () {
$("#<%=btA.ClientID%>").trigger('click');
}, 10);
回答by John Gathogo
Its beats me since it should be a very straightforward thing. I actually just tried it out and it worked without a hitch. Here is my markup:
它击败了我,因为它应该是一件非常简单的事情。我实际上只是试了一下,它工作顺利。这是我的标记:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%=btA.ClientID%>").trigger('click');
});
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbA" runat="server"></asp:Label>
<asp:Button ID="btA" runat="server" OnClick="btA_Click" Text="Click Me!" />
</div>
</form>
</body>
</html>
... and I have this method in my code behind:
...我在我的代码后面有这个方法:
protected void btA_Click(object sender, EventArgs e)
{
lbA.Text = "Hello World!";
}
When the application runs, it triggers the click
event of the btA
button fires immediately and the Hello World!text is rendered on the label. Check if you could be missing something.
当应用程序运行时,它会立即触发按钮click
事件btA
和Hello World! 文本呈现在标签上。检查您是否可能遗漏了什么。
回答by Joebet Mutia
I always make ClientIdMode="static" so that you can easily call the element with the same id you configured in the page..
我总是使 ClientIdMode="static" 以便您可以轻松地使用您在页面中配置的相同 ID 调用元素..
So you dont hvae to use ClientID.. More over you cant use asp.net code in a separate js file.
所以你不能使用 ClientID .. 更重要的是你不能在单独的 js 文件中使用 asp.net 代码。
$("#<%=btA.ClientID%>").trigger('click');
$("#<%=btA.ClientID%>").trigger('click');
to
到
$("#btA").click();
$("#btA").click();