jQuery和ASP.NET控件问题

LB.*_*LB. 1 javascript asp.net jquery

如果我有一个自定义ASP.NET控件作为带有ID的html控件呈现,我试图用jQuery向输入控件添加一个属性.

但是,我遇到了一些问题:

首先,我的jQuery无法选择它.

我到目前为止:

$(document).ready(function() {
  $(client id of the input control).attr('onclick', function () { alert('hey!'); });
});
Run Code Online (Sandbox Code Playgroud)

似乎jQuery试图找到输入控件但不能.

有任何想法吗?

更新:

通常,提供的解决方案可行,但由于这是一个SharePoint Publishing .aspx页面,因此不允许使用代码块...尝试其他解决方法.

Gro*_*mer 5

只需在jQuery中连接click事件.

$(document).ready(function() {
    $(id of the input control)click(function() {
        alert('hey!');
    });
});
Run Code Online (Sandbox Code Playgroud)

此外,请确保您不只是将您键入的ID放入jQuery中的控件中.ASP.Net使用与给定控件不同的ID来呈现控件.在我运行的其中一个网站上,我有以下按钮:

<asp:Button ID="btnSignup" runat="server" Text="Sign Up" />
Run Code Online (Sandbox Code Playgroud)

呈现的HTML中的ID不是btnSignup,它实际上呈现为ctl00_cpLeft_btnSignup.要使用jQuery选择该控件,您可以这样做:

$("input[id$='btnSignup'").click(function() { ... });
Run Code Online (Sandbox Code Playgroud)

编辑:

$("input[id$='btnSignup'}").click(function() { ... });
Run Code Online (Sandbox Code Playgroud)

您也可以选择使用:

$("#<%= btnSignup.ClientID %>").click(function() { ... });
Run Code Online (Sandbox Code Playgroud)

编辑:

我看着使用<%= ...%>方法,你就必须有你的JavaScript IN的ASPX/ASCX文件本身为它工作.因此,我坚持使用正则表达式方法.