jQuery和asp.net不会合作

时间:2013-06-19 11:25:33

标签: javascript jquery asp.net

我只是想让jQuery在我的页面上运行,但它并不是那么顺利。 我尝试了很多不同的解决方案,我在网上找到了,但到目前为止,唯一有效的解决方案是代码隐藏。而且只有“警觉”没有别的。

button1.Attributes.Add("onclick", "alert(\"hello\")");
页眉中的

<script type="text/javascript" src="../../scripts/jquery-1.5.1.js"></script>
<script type="text/javascript">
   $(document).ready(function () {
      $("#button1").click(function(event){
         alert("Hello world!");
      });
   });
</script>
页面正文中的

<asp:Button ID="button1" runat="server" Text="button" />

我拿到了按钮,但点击它时不会发生该死的事。

修改

非常感谢快速回答"#<%=button1.ClientID %>"解决了我的问题,但是新问题已经证明了这一点。

$(document).ready(function () {
   $("#<%=button1.ClientID %>").click(function () {
      $("#<%=div1.ClientID %>").fadeOut();
   });
});

它说“div1”不存在(虽然它确实存在)

1 个答案:

答案 0 :(得分:0)

你不需要这个

button1.Attributes.Add("onclick", "alert(\"hello\")");

请检查以下代码。

<script type="text/javascript">
 $(document).ready(function () {

   //client id************
   $("#<%=button1.ClientID %>").click(function(event){
      alert("Hello world!");
   });
  });
</script>

它会起作用。如果您在浏览器中检查您的html源代码,您将看到button1被更改。如果您使用clientid,您将解决您的问题