为什么jQuery是不asp.net工作? [英] Why Jquery is not working with asp.net?

查看:82
本文介绍了为什么jQuery是不asp.net工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不工作,我不明白为什么,我已经试过了W3Schools的code和这样的作品,我想问题可能是参考什么的,我是新来ASP.NET

ASP code(母版页)

 <脚本SRC =./脚本/ jQuery的-1.4.1.js类型=文/ JavaScript的>< / SCRIPT>
  <脚本类型=文/ JavaScript的>      $(文件)。就绪(函数(){
          $(#bt_insertar)。点击(函数(){
              警报(处理器为。点击()调用。);
          });
      });                < / SCRIPT>
< ASP:的ContentPlaceHolder ID =头=服务器>
< / ASP:&的ContentPlaceHolder GT;

Index.aspx的

 < TD类=都市风情>
            < ASP:按钮的ID =bt_insertar=服务器文本=InsertarWIDTH =71px
                的onclick =bt_insertar_Click的风格=高度:26px/>
        < / TD>


解决方案

默认情况下, ID 你给你的 =服务器控件不是 ID 的DOM元素上结束。你可以得到ASP.Net从生成ID 客户端ID 服务器端控制对象属性。例如,更改:

  $(#bt_insertar)。单击(...

  $(#<%= bt_insertar.ClientID%GT;)。单击(...

...如果code是在一个页面ASP.Net解析(相对于外部JavaScript文件)。

由于ASP.Net 4,可以控制通过的 的ClientIDMode 属性。例如,如果您使用 静态 模式( control.ClientIDMode = CLientIDMode.Static; ),那么 ID 其实是在通过原样通过。但对于默认值的ClientIDMode predictable ,它会修改 ID 您使用。

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 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

 <td class="style4">
            <asp:Button ID="bt_insertar" runat="server" Text="Insertar" Width="71px" 
                onclick="bt_insertar_Click" style="height: 26px" />
        </td>

解决方案

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(...

to

$("#<%= bt_insertar.ClientID%>").click(...

...if that code is in a page ASP.Net parses (as opposed to an external JavaScript file).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆