jQuery.change +内嵌的onchange FN()导致FN执行两次 [英] jQuery.change + inline onchange fn() causes fn to execute twice

查看:436
本文介绍了jQuery.change +内嵌的onchange FN()导致FN执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发布这个即使我找到了答案和解决方法,因为它可以帮助别人的希望。

Posting this even though I found the answer and a workaround, in the hopes that it helps someone else.

基本上,使用的 IE8 并更改元素的具有价值当两个 jQuery.change 迷上它和内嵌的onchange 定义,< STRONG>平变化中的code执行两次。

Basically, when using IE8 and changing an element's value that has both jQuery.change hooked to it and an inline onchange defined, the code in onchange executes twice.

我们遇到了这个在 ASP.NET(3.5或4.0 无所谓)是的jQuery 1.4.2 的AutoPostBack =真控件,它呈现出与的onchange =__ doPostBack(...)属性,导致回发为单个用户启动的控制状态变化发生两次。 (微软真的需要摆脱浏览器的企业 - 他们在这可怕的)

We ran into this in an ASP.NET (3.5 or 4.0 does not matter) project with jQuery 1.4.2 and AutoPostBack=true controls, which render out with onchange="__doPostBack(...)" attributes, causing the postback to happen twice for a single user-initiated control-state change. (Microsoft really needs to get out of the browser business -- they're awful at it.)

要演示这个问题,我创建了一个空的网站项目,并添加一个aspx页面,完整code +标记下方。最重要的元素是脚本引用jQuery的,即电线了control.change处理程序,而孤独的自动回控制(我已经验证,这与两个文本框和DropDownList的发生)的$的document.ready。需要注意的是在页面上不存在其他控制 - 无AJAXToolkit控制,像的UpdatePanel,没有IMGS空SRC attribs,这是一个长期存在的错误,也触发双回发。

To demo the problem, I created an empty website project and added a single aspx page, complete code+markup below. The important elements are the SCRIPT reference to jQuery, the $document.ready that wires up the control.change handler, and the lone autopostback control (I've verified that it happens with both TextBox and DropDownList). Note that no other controls exist on the page -- no AJAXToolkit controls, like UpdatePanels, and no IMGs with empty SRC attribs, which is a long-standing bug that also triggers double postback.

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" 
    EnableSessionState="False" EnableViewState="false" %>

<script runat="server">
    protected void Page_Init(object sender, EventArgs e)
    {
        Trace.Write(String.Format("IsPostBack = {0}; PostbackControl = {1}",      
          IsPostBack.ToString(), Page.Request.Params.Get("__EVENTTARGET")));
    }

    protected void txtTest_TextChanged(object sender, EventArgs e)
    {
        Trace.Write(String.Format("Name = {0}", txtTest.Text));
    }
</script>

<html>
  <head>
    <title>Demo of jQuery + AutoPostBack double postback problem</title>
  </head>
  <body>
    <form id="Form1" runat="server">

      <script src="jquery-1.4.2.js" type="text/javascript"></script>
      <script type="text/javascript">
          $(document).ready(function () {
              $('#<%= txtTest.ClientID %>').change(function () { 
                alert('Double postback coming up!'); 
              })
          });
      </script>

      <asp:TextBox ID="txtTest" runat="server" AutoPostBack="true" 
        OnTextChanged="txtTest_TextChanged" />

    </form>
  </body>
</html>

您可以通过设置Page_Init断点,使I​​E脚本调试,并通过JavaScript步进看到的onchange执行两次,或启用跟踪和观察的trace.axd所产生的两个请求观察双回发。

You can observe the double postback either by setting a breakpoint in Page_Init, enabling IE script debugging and stepping through javascript to see onchange execute twice, or by enabling trace and viewing the resulting two requests in trace.axd.

这不要紧,你把什么jQuery.change处理程序中;除去警报或有它返回一个布尔值,甚至使得处理程序完全是空的有双重的onchange /回发的行为没有任何影响。

It does not matter what you put inside the jQuery.change handler; removing the alert or having it return a bool or even making the handler completely empty has no impact on the double onchange/postback behavior.

这也恰好不管你是否wireup服务器端的框TextChanged事件处理程序声明中标记或命令在code(显然你不应该做的全部,或者将导致双回送自己)。

It also happens regardless of whether you wireup the server-side TextChanged eventhandler declaratively in markup or imperatively in code (obviously you shouldn't do both or you would be causing the double postback yourself).

虽然ASP.NET平变化产生=__ doPostBack(...)正在执行两次,jQuery.change处理器只执行一次,因为警报只显示一次。偶尔(也许1出5或6的通行证),仅平变化触发单的时候,回发一次,所以这个bug似乎并不一致体现。

Even though the ASP.NET-generated onchange="__doPostBack(...)" is executing twice, the jQuery.change handler is only executing once because the alert only displays once. Occasionally (maybe 1 out of 5 or 6 passes), onchange only fires a single time, posting back once, so the bug doesn't seem to manifest consistently.

更糟糕的是,在实际生产项目,我们第一次遇到这种行为,有时当它的双帖,的IsPostBack =真为一个请求和假为别的,造成初始化code再次执行。但我一直无法重现演示项目,该项目的行为,所以它可能是一个单独的问题这一个互动。

Even worse, in the actual production project where we first encountered this behavior, sometimes when it double posts, IsPostBack=true for one request and false for the other, causing initialization code to execute again. But I have not been able to reproduce that behavior in the demo project, so it may be a separate issue interacting with this one.

如果您注释掉jQuery.change,它会恢复到正常所需的单回发的行为。

If you comment out the jQuery.change, it reverts to the normal desired single postback behavior.

我GOOGLE了像疯了似的,但只有永远能够找到一个单一的论坛上发帖说 MIGHT 的一直面临着同样的问题。只有当我决定在Firefox没有发生问题,并添加IE8我的搜索字词的名单是我遇到jQuery的bug报告运行。
http://dev.jquery.com/ticket/6310
http://dev.jquery.com/ticket/6593

I googled like crazy but was only ever able to find a single forum post that MIGHT have been facing the same issue. Only after I determined that the problem did not occur in Firefox and added IE8 to my list of search terms did I run across the jQuery bug reports. http://dev.jquery.com/ticket/6310 http://dev.jquery.com/ticket/6593

解决方法(在ASP.NET)是禁用的AutoPostBack和jQuery的事件处理器对于必须运行的客户端和服务器端的code所有控件的末尾明确添加__doPostBack。

The workaround (in ASP.NET) is to disable AutoPostBack and to explicitly add __doPostBack at the end of the jQuery eventhandlers for all controls that must run both client- and server-side code.

编辑:修正了jQuery选择,正如MEEP指出的那样,是缺少#

Fixed the jQuery selector which, as meep pointed out, was missing the #.

推荐答案

微软扑一边,这更多的是jQuery的错,然后IE的。它是由jQuery的企图绕开缺乏冒泡的变更事件在IE造成的,调用额外的伪造更改事件每个祖先元素以及目标元素,除了自然变化的事件。 presumably的bug被忽视了作为触发多个的onchange 事件通常是无害的。

Microsoft-bashing aside, this is more jQuery's fault then IE's. It's caused by jQuery's attempt to work around the lack of bubbling for the change event in IE, calling extra faked change events for each ancestor element as well as the target element, in addition to the natural change event. Presumably the bug went unnoticed as triggering multiple onchange events would normally be harmless.

(这不是一个答案。不过,你的问题不是一个问题。)

(This isn't an answer. But then, your question isn't a question.)

这篇关于jQuery.change +内嵌的onchange FN()导致FN执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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