javascript 为什么 jQuery 不能与 ASP.NET 一起工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17632425/
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
Why isn't jQuery working with ASP.NET for me?
提问by Molinator
This doesn't work and I don't understand why, I have tried that code on W3schools and that works, I think the problem might be the reference or something, I'm new to ASP.NET.
这不起作用,我不明白为什么,我已经在 W3schools 上尝试过该代码并且有效,我认为问题可能出在参考或其他方面,我是 ASP.NET 的新手。
ASP Code(Master Page)
ASP 代码(母版页)
<script src="./Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#bt_insertar").click(function () {
alert("Handler for .click() called.");
});
});
</script>
<asp:ContentPlaceHolder id="head" runat="server"></asp:ContentPlaceHolder>
Index.aspx
索引.aspx
<td class="style4">
<asp:Button ID="bt_insertar" runat="server" Text="Insertar" Width="71px"
onclick="bt_insertar_Click" style="height: 26px" />
</td>
回答by T.J. Crowder
By default, the ID
you give your runat="server"
control isn't the id
that ends up on the DOM element. You can get the ID that ASP.Net generates from ClientID
property on the server-side control object. E.g., change:
默认情况下,ID
您提供的runat="server"
控制权不是id
最终在 DOM 元素上的。您可以从ClientID
服务器端控件对象的属性中获取 ASP.Net 生成的 ID 。例如,改变:
$("#bt_insertar").click(...
to
到
$("#<%= bt_insertar.ClientID%>").click(...
...if that code is in a page ASP.Net parses (as opposed to an external JavaScript file).
...如果该代码位于 ASP.Net 解析的页面中(而不是外部 JavaScript 文件)。
As of ASP.Net 4, you can control this behavior via the ClientIDMode
property. For instance, if you use the Static
mode (control.ClientIDMode = CLientIDMode.Static;
), then the ID
is in fact passed through as-is. But the default value for ClientIDMode
is Predictable
, which modifies the ID
you use.
从 ASP.Net 4 开始,您可以通过ClientIDMode
属性控制此行为。例如,如果您使用Static
模式 ( control.ClientIDMode = CLientIDMode.Static;
),那么ID
实际上是按原样传递的。但默认值ClientIDMode
是Predictable
,它会修改ID
您使用的 。
回答by Raghubar
To Bind jquery function to Asp.net Server control you have to write code this way.
要将 jquery 函数绑定到 Asp.net Server 控件,您必须以这种方式编写代码。
<script type="text/javascript">
$(document).ready(function () {
$('#<%= bt_insertar.ClientID %>').click(function () {
alert("Handler for .click() called.");
});
});
回答by Anton Belev
Several points:
几点:
- In your JQuery include statement the src should be
src="../Scripts/jquery-1.4.1.js"
instead ofsrc="./Scripts/jquery-1.4.1.js"
- You don't really need to bind click event in your
document.ready
event instead you can includeOnClientClick="bt_insertar_Click();"
in yourasp:Button
- 在您的 JQuery 包含语句中 src 应该是
src="../Scripts/jquery-1.4.1.js"
而不是src="./Scripts/jquery-1.4.1.js"
- 你真的不需要在你的
document.ready
事件中绑定点击事件,而是你可以包含OnClientClick="bt_insertar_Click();"
在你的asp:Button
and then you can define your function bt_insertar_Click()
然后你可以定义你的 function bt_insertar_Click()
<script type="text/javascript">
function bt_insertar_Click() {
alert("Handler for .click() called.");
}
</script>
It is a better programming practice to use a separate .js file for you web page so you can move all the relevant js functions to this js file and then include it like you did with the jquery file.
为您的网页使用单独的 .js 文件是一种更好的编程实践,这样您就可以将所有相关的 js 函数移动到这个 js 文件中,然后像使用 jquery 文件一样包含它。