jQuery asp:按钮上的jQuery单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/716827/
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
jQuery Click event on asp:button
提问by Sachin Gaur
I have a server side button as
我有一个服务器端按钮
<asp:Button ID="btnSummary" runat="server" OnClick="btnSummary_Click" Text="Next" />
I want to attach the jQuery Click event using its ID and NOT using the alternative class attribute way.
I tried to attach the click event as:
我想使用其 ID 附加 jQuery Click 事件,而不是使用替代类属性方式。
我尝试将点击事件附加为:
$("#btnSummary").click(function()
{
alert("1");
});
But, its click event is not fired. Also, I have also tried $("id[$btnSummary]")
.
Is there any way to attach the click event on asp:button using jQuery without the class attribute on the button?
但是,它的点击事件不会被触发。另外,我也试过了$("id[$btnSummary]")
。
有什么方法可以在没有按钮上的 class 属性的情况下使用 jQuery 在 asp:button 上附加 click 事件?
回答by Russ Cam
Some of the options you have can be found here
您可以在此处找到一些选项
How to stop ASP.NET from changing ids in order to use jQuery
如何阻止 ASP.NET 更改 ids 以使用 jQuery
EDIT:
编辑:
After reading your comments on other answers, it sounds like you need to bind the onclick event handler inside of ASP.NET AJAX's pageLoad, so that it is bound on every postback, including asynchronous postbacks. On the other hand, $(document).ready() binds only once on initial page load
阅读您对其他答案的评论后,听起来您需要在 ASP.NET AJAX 的pageLoad内绑定 onclick 事件处理程序,以便在每次回发(包括异步回发)上绑定它。另一方面, $(document).ready() 在初始页面加载时只绑定一次
function pageLoad(sender, args)
{
$("#<%=btnSummary.ClientID %>").click(function()
{
alert("1");
});
}
Dave Ward wrote a nice article on the difference between pageLoad and $(document).ready().
Dave Ward 写了一篇关于 pageLoad 和 $(document).ready() 区别的好文章。
回答by Luis Coell
Add ClientIDMode="Static" to your asp:button, something like this:
将 ClientIDMode="Static" 添加到您的 asp:button,如下所示:
<asp:Button ID="Button1" runat="server" ClientIDMode="Static" Text="Button" />
This will make the ID remain the same. It disables the autogenerated names for this control.
这将使 ID 保持不变。它禁用此控件的自动生成的名称。
Here is the reference for this: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
这是对此的参考:http: //msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
回答by ólafur Waage
- Check if the id attribute is in the html source when you run the site.
- Do you have the click function inside a document ready function ?
- 运行站点时检查 id 属性是否在 html 源代码中。
- 您在文档就绪功能中有点击功能吗?
-
——
$(document).ready(function() {
// put all your jQuery goodness in here.
});
EDIT:
编辑:
Since its a server side control and the ID changes, you will need to dynamically update the javascript on every page load with the variable.
由于它是服务器端控件并且 ID 发生变化,因此您需要在每次加载页面时使用变量动态更新 javascript。
回答by GeoffD
Assigning the selector to the class worked for me.
将选择器分配给班级对我有用。
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="My Button" CssClass="myButton"/>
<script>
jQuery(".myButton").click(function () {
alert("clicked");
});
</script>
回答by user3615318
Please use the following syntax : $("[id*=Button1]")
to reference any asp control
请使用以下语法:$("[id*=Button1]")
引用任何asp控件
回答by John Leidegren
ASP.NET generates a UniqueID for client-side stuff, you can use that to bind the event. That ID is generated based on the position of that Control inside a ControlCollection and different INamingContainers, it's ugly and you can't guess it...
ASP.NET 为客户端的东西生成一个 UniqueID,你可以使用它来绑定事件。该 ID 是根据 ControlCollection 和不同 INamingContainers 中该 Control 的位置生成的,它很丑陋,您无法猜到...
Add this kind of code somewhere on your page to hook up that button.
在页面上的某处添加这种代码以连接该按钮。
<script type="text/javascript">
$(function() { $("#<%=btnSummary.ClientID%>") }).click(function(){/*...*/});
</script>
回答by ra170
I'm little confused here now. Let me explain:
我现在在这里有点困惑。让我解释:
1. I added a button on the page:
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<div>
2. Then I added a JavaScript and jQuery:
<script type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function() {
alert("Hello world!");
});
});
</script>
3. The generated html is this:
<div>
<input type="submit" name="Button1" value="Button" id="Button1" />
<div>
Now, I don't see ASP.NET (asp.net 3.5) changing the ids. Why do I see different behavior?
现在,我没有看到 ASP.NET (asp.net 3.5) 更改 id。为什么我看到不同的行为?
Btw. This does work when I hit the button!
顺便提一句。当我按下按钮时,这确实有效!
Thanks.
谢谢。
回答by Bless Yahu
ASP.NET adds to you id (example: id "selectButton
" becomes
ASP.NET 向您添加 id(例如:id " selectButton
" 变为
"ctl00_middleContent_gvPeople_ctl04_selectButton");
Use the jquerysyntax to search for the part of the id that doesn't change.
使用jquery语法搜索id不变的部分。
$("[id='_selectButton']")
回答by Alex Stephens
use pageLoad when using updatepanels because document.ready only runs once on initialization of the page and loses its binding on partial postbacks. PageLoad gets run everytime so will rebind every partial postback.
在使用 updatepanels 时使用 pageLoad,因为 document.ready 仅在页面初始化时运行一次,并且在部分回发时失去其绑定。PageLoad 每次都会运行,因此会重新绑定每个部分回发。
回答by Peter
I had the same problem, this works for me
我有同样的问题,这对我有用
<asp:Button ID="Button1" runat="server" Text="ASP Button" OnClientClick="return false;" />
Its the addition of return false that seems to make the difference. Hope this helps.
它的添加 return false 似乎有所作为。希望这可以帮助。
regards
问候
Peter
彼得