jQuery 点击消失进入深渊

发布于 2024-10-02 03:36:59 字数 1968 浏览 0 评论 0原文

这是我的设置:

我的页面上有一个 asp.net 按钮 -

<asp:Button id="btnSelectEmp" runat="server" Text="Select Employee" />

我有一个带有以下 jQuery 单击事件的 .js 文件 -

$("input[id$='_btnSelectEmp']").click(function ($e) {
  $("div[id$='_divEmpSearch']").css("display", "inline");
  $e.preventDefault();
});

如您所见,单击该按钮将设置一个可见的 div。没什么特别的;不是火箭科学。

div 包含一个 asp.net 更新面板,并且它包含一个 asp.net 用户控件 (.ascx)

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
  <div id="divEmpSearch" runat="server" style="display: none;">
    <uc:EmpSearch ID="ucEmpSearch" runat="server" />
  </div>
  // And a bunch of other controls that are updated according to whatever the user selects in the user control above
</ContentTemplate>
<Triggers>
  <asp:AsyncPostBackTrigger ControlID="ucEmpSearch" />
</Triggers>
</asp:UpdatePanel>

上面的用户控件也包含在一个 asp.net 更新面板中,因为它必须与服务器通信。在文本框等其他控件中,用户控件上有两个按钮:1) 执行回发的 asp.net 按钮,2) 执行异步回发的 asp.net 按钮。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
      <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /
      <br />
      asp:Button ID="btnContinue" runat="server" Text="Select" OnClick="btnContinue_Click" />
    </ContentTemplate>
    <Triggers>
      <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
      <asp:PostBackTrigger ControlID="btnContinue" />
    </Triggers>
</asp:UpdatePanel>

用户控件中执行回发的按钮运行良好。我单击它,发生回发并且我的 div 控件被重新隐藏。然后,我可以单击“选择员工”按钮(我在问题的第一个问题中提供了代码的按钮),然后 jQuery 单击事件将被处理,div 将重新显示。

但是,用户控件中执行异步回发的按钮也可以工作,但在隐藏 div 后,如果我单击“选择员工”按钮,则 jQuery 单击事件将不会被处理

这告诉我,由于某种原因,在异步回发到页面期间,“选择员工”按钮发生了一些情况,因此 jQuery 单击事件不再发生。为什么?

Here is my setup:

I have an asp.net button on a page --

<asp:Button id="btnSelectEmp" runat="server" Text="Select Employee" />

I have a .js file with the following jQuery click event --

$("input[id$='_btnSelectEmp']").click(function ($e) {
  $("div[id$='_divEmpSearch']").css("display", "inline");
  $e.preventDefault();
});

As you can see, clicking upon the button will set a div visible. Nothing special; not rocket science.

The div is wrapped with an asp.net update panel, and it contains an asp.net user control (.ascx)

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
  <div id="divEmpSearch" runat="server" style="display: none;">
    <uc:EmpSearch ID="ucEmpSearch" runat="server" />
  </div>
  // And a bunch of other controls that are updated according to whatever the user selects in the user control above
</ContentTemplate>
<Triggers>
  <asp:AsyncPostBackTrigger ControlID="ucEmpSearch" />
</Triggers>
</asp:UpdatePanel>

The user control above is also wrapped in an asp.net update panel, because it has to communicate with the server. Among other controls like textboxes and such, the user control has two buttons upon it: 1) an asp.net button that does a postback and 2) an asp.net button that does an asynchronous postback.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
      <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /
      <br />
      asp:Button ID="btnContinue" runat="server" Text="Select" OnClick="btnContinue_Click" />
    </ContentTemplate>
    <Triggers>
      <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
      <asp:PostBackTrigger ControlID="btnContinue" />
    </Triggers>
</asp:UpdatePanel>

The button in the user control that does a postback is working great. I click it, a postback occurs and my div control is re-hidden. I can then click on the Select Employee button (the one that I supplied the code for at the very first of the question) and the jQuery click event is handled and the div will be reshown.

However, the button in the user control that does an asynchronous postback works also, but after it hides the div, if I then click on the Select Employee button the jQuery click event will not be handled.

What this tells me is that for some reason during an asynchronous postback to the page, something happens to the Select Employee button so that the jQuery click event no longer happens. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ζ澈沫 2024-10-09 03:36:59

当您的更新面板返回新内容时,您的按钮将被新按钮替换,因此:

$("input[id$='_btnSelectEmp']").click(function ($e) {

绑定到它当时找到的元素,而不是您需要 .delegate().live() 用于监听当前未来元素的click事件,如下所示:

$("input[id$='_btnSelectEmp']").live("click", function ($e) {
  $("div[id$='_divEmpSearch']").css("display", "inline");
  $e.preventDefault();
});

或者使用 .delegate() 便宜一点:

$("#container").delegate("input[id$='_btnSelectEmp']", "click", function ($e) {
  $("div[id$='_divEmpSearch']").css("display", "inline");
  $e.preventDefault();
});

在这种情况下 < code>#container 应该是更新面板的父级,在回发中不会被替换。

Your button is replaced with a new one when your update panel comes back with new content, so this:

$("input[id$='_btnSelectEmp']").click(function ($e) {

Binds to the elements it finds at that time, instead you'll want .delegate() or .live() here to listen for click events from current and future elements, like this:

$("input[id$='_btnSelectEmp']").live("click", function ($e) {
  $("div[id$='_divEmpSearch']").css("display", "inline");
  $e.preventDefault();
});

Or a bit cheaper using .delegate():

$("#container").delegate("input[id$='_btnSelectEmp']", "click", function ($e) {
  $("div[id$='_divEmpSearch']").css("display", "inline");
  $e.preventDefault();
});

In this case #container should be a parent of the update panel, one that doesn't get replaced in the postback.

晨曦÷微暖 2024-10-09 03:36:59

使用live()函数。 live() 将点击事件委托给父元素,因此该元素(在本例中为 btnSelectEmp)在绑定事件时不需要存在。

$("#<%=btnSelectEmp.ClientID%>").live("click" function ($e) {
  $("#<%=divEmpSearch.ClientID%>").css("display", "inline");
  $e.preventDefault();
});

发生的情况是 btnSelectEmp 按钮被异步调用替换,并且新元素尚未绑定到事件处理程序。

另外,我在这里修改了 jquery 选择器以使用元素的确切客户端 ID。这将提高速度,而且我似乎记得某些选择器在某些版本的 Jquery 中不能与事件委托一起使用。

Use the live() function. live() delegates the click event to a parent element, so the element (in this case btnSelectEmp) doesn't need to exist at the time the event is bound.

$("#<%=btnSelectEmp.ClientID%>").live("click" function ($e) {
  $("#<%=divEmpSearch.ClientID%>").css("display", "inline");
  $e.preventDefault();
});

What is happening is the btnSelectEmp button is getting replaced by the asynchronous call and the new element has not been bound to an event handler.

Also, I've modified the jquery selector here to use the exact client id of the element. This will improve speed, plus I seem to recall certain selectors don't work with event delegation in certain versions of Jquery.

时光暖心i 2024-10-09 03:36:59

尝试使用 live('click', function(){...}) 而不是 click(function(){...})

try live('click', function(){...}) instead of click(function(){...})

无远思近则忧 2024-10-09 03:36:59

疯狂猜测:“_btnSelectEmp”被使用多次?

Wild guess : "_btnSelectEmp" is used more than once?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文