简体   繁体   English

jQuery OnClick没有触发我的事件

[英]jQuery OnClick not firing my event

I have a jQuery which is like this: 我有一个这样的jQuery:

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

       $("#txtAgentName").click(function (event) {

           alert("I am ready!");

       });

   });
</script>

What I am assuming that this should fire if I click on my textbox txtAgentName. 我假设如果单击文本框txtAgentName,则应该触发。 But it is not happening. 但这没有发生。

This is my textbox markup: 这是我的文本框标记:

 <asp:TextBox ID="txtAgentName" runat="server" onclick="Event();"></asp:TextBox>

However, when I am doing this on my Javascript: 但是,当我在Javascript上执行此操作时:

function ShowTime() {
         alert('<%= DateTime.Now.ToString() %>');
       }

and calling this ShowTime from my TextBox OnClick event, then it is firing up. 然后从我的TextBox OnClick事件中调用此ShowTime,然后启动。 Dont know what I am doing wrong here :( 不知道我在做什么错了:(

  1. In ASP.NET setting the ID doesn't mean the DOM element will represent the same ID 在ASP.NET中,设置ID并不意味着DOM元素将代表相同的ID
  2. You have an onclick event handler, try removing that. 您有一个onclick事件处理程序,请尝试将其删除。

Instead I would add a class onto it and do something like 相反,我会在上面添加一个类并执行类似的操作

<asp:TextBox ID="txtAgentName" runat="server" CssClass="my-text-box"></asp:TextBox>

$(function() {
    $('.my-text-box').click(function() {
        alert('Clicked!');
    });
});

The ID property of a control in ASP.NET WebForms is what will get used when building the control tree. ASP.NET WebForms中控件的ID属性是在构建控件树时将使用的属性。 If you look at the actualy ID in the DOM it probably says something like MainContent_txtAgentName instead. 如果您查看DOM中的实际ID,它可能会说类似MainContent_txtAgentName

You might want also to take a look at setting the ClientIDMode http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx 您可能还想看看如何设置ClientIDMode http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4。 aspx

If you're using ASP.NET 4.0 you can use <asp:TextBox ID="txtAgentName" runat="server" ClientIDMode="Static" ></asp:TextBox> this will render verbatim what you have in the ID attribute as the actual ID on the DOM. 如果您使用的是ASP.NET 4.0,则可以使用<asp:TextBox ID="txtAgentName" runat="server" ClientIDMode="Static" ></asp:TextBox>来逐字呈现ID属性中的内容DOM上的实际ID。

When that web control renders, it is given an auto generated id. 当该Web控件呈现时,会为其提供一个自动生成的ID。 Which means the jquery selector never finds it. 这意味着jquery选择器永远找不到它。 In addition to what has already been mentioned, you can use 除了已经提到的内容,您还可以使用

$("#<%= txtAgentName.ClientId %>").click(function (event) {

Or change the control to have the following attribute 或将控件更改为具有以下属性

ClientIDMode="Static"

which will tell it to render txtAgentName as the id. 它将告诉它呈现txtAgentName作为ID。

It is because .Net is probably renaming your textbox. 这是因为.Net可能正在重命名您的文本框。 Look at your generated code in the browser at what the textbox is getting renamed to. 在浏览器中查看您生成的代码,将文本框重命名为什么。 It'll be a mess. 一团糟。 If you are using ASP.NET4, you can add ClientIdMode="Static" to the control, and it will remain named as you desire. 如果您使用的是ASP.NET4,则可以将ClientIdMode =“ Static”添加到控件中,并且它将根据您的需要保持命名。 Otherwise, your best bet is to give the textbox a class, and use that as the selector. 否则,最好的选择是给文本框一个类,并将其用作选择器。

One other thing you could do is use <%= txtAgentName.ClientId %> to get the Client id. 您可以做的另一件事是使用<%= txtAgentName.ClientId%>获取客户端ID。 But if your jQuery is not on that page, then that will not work. 但是,如果您的jQuery不在该页面上,那将不起作用。

Try this: 尝试这个:

 <asp:TextBox CssClass="myAgent" ID="txtAgentName" runat="server"></asp:TextBox>


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

   $(".myAgent").click(function (event) {

       alert("I am ready!");

   });

});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM