为什么jQuery不能和我一起使用ASP.NET? [英] Why isn't jQuery working with ASP.NET for me?
问题描述
这不起作用,我不明白为什么,我在W3schools上尝试过这个代码并且有效,我认为这个问题可能是参考或其他什么,我是ASP.NET的新手。
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.
ASP代码(母版页)
ASP Code(Master Page)
<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
Index.aspx
<td class="style4">
<asp:Button ID="bt_insertar" runat="server" Text="Insertar" Width="71px"
onclick="bt_insertar_Click" style="height: 26px" />
</td>
推荐答案
默认情况下, ID
你给你的 runat =server
控件不是最终的 id
在DOM元素上。您可以从获取ASP.Net生成的ID服务器端控件对象上的 ClientID
属性。例如,更改:
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:
$("#bt_insertar").click(...
到
$("#<%= bt_insertar.ClientID%>").click(...
...如果该代码在页面ASP.Net中解析(而不是外部JavaScript文件)。
...if that code is in a page ASP.Net parses (as opposed to an external JavaScript file).
从ASP.Net 4开始,您可以通过<控制此行为a href =http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx =nofollow> ClientIDMode
属性。例如,如果你使用 静态
模式( control.ClientIDMode = CLientIDMode.Static;
),然后 ID
实际上是按原样传递的。但是 ClientIDMode
的默认值是可预测
,修改你使用的 ID
。
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.
这篇关于为什么jQuery不能和我一起使用ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!