Html Select List:为什么onchange会被调用两次? [英] Html Select List: why would onchange be called twice?

查看:214
本文介绍了Html Select List:为什么onchange会被调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有选择列表(ASP.NET MVC页面)的页面

选择列表和onchange事件是这样指定的:

 <%= Html.DropDownList(CompanyID,Model.CompanySelectList,(select company),new {@class =data-entry-字段,@onchange =companySelectListChanged()})%> 

companySelectListChanged函数被调用了两次?

我使用这个问题中的漂亮代码来获取调用者。



调用者都是onchange事件,但是如果我使用以下方法查看调用者:

  arguments.callee.caller.caller 

第一调用返回一些系统代码作为调用者(我假设),第二次调用返回undefined。



我正在检查未定义,只对onchange作出一次反应,但这似乎并不理想的情况下,谁会在onchange被称为两次?



更新: 找到罪魁祸首! ...除了我:-),但调用companySelectListChanged函数两次的问题仍然存在。



onchange事件直接在select上设置,如前所述。这会调用companySelectListChanged函数。

..注意'data-entry-field'类,现在在一个单独的链接的javascript文件中包含此类的所有字段上的更改事件绑定到一个可以改变保存按钮颜色的函数。这意味着onchange有两个事件,但companySelectListChanged被调用两次?



额外的绑定设置如下:















假设可能在选择列表上有两个改变事件,它将假定设置按键&关键事件可能会破坏某些东西?



任何想法? 其他更新:

select元素看起来很好,如果我删除额外的更改绑定,所有工作。当添加'change'绑定时,该事件触发一次,硬连线'onchange'被触发两次。



如果两个事件都通过jQuery绑定,似乎混合硬连线onchange和jquery绑定的变化事件不能混合?我想这回答了我的问题,但似乎像IE的问题,并绑定这些事件与jQuery。

解决方案

我同意你的评估。我已经在 http://gutfullofbeer.net/onchange.html 更新了我的小示例,其中包括jQuery处理程序除了DOM 0处理程序(使用onchange属性设置的处理程序)之外。它似乎是一个jQuery错误,或者至少是jQuery处理IE怪异的失败。



我记录了jQuery ticket 6593。


I have a page with a select list (ASP.NET MVC Page)

The select list and onchange event specified like this:

<%=Html.DropDownList("CompanyID", Model.CompanySelectList, "(select company)", new { @class = "data-entry-field", @onchange = "companySelectListChanged()" })%>

The companySelectListChanged function is getting called twice?

I am using the nifty code in this question to get the caller.

both times the caller is the onchange event, however if i look at the callers caller using:

arguments.callee.caller.caller

the first call returns some system code as the caller (i presume) and the second call returns undefined.

I am checking for undefined to only react once to onchange, but this doesnt seem ideal, whcy would onchange be called twice?

UPDATE:

ok, found the culprit! ...apart from me :-) but the issue of calling the companySelectListChanged function twice still stands.

The onchange event is set directly on the select as mentioned. This calls the companySelectListChanged function.

..notice the 'data-entry-field' class, now in a separate linked javascript file a change event on all fields with this class is bound to a function that changes the colour of the save button. This means there is two events on the onchange, but the companySelectListChanged is called twice?

The additional binding is set as follows:

$('.data-entry-field').bind('keypress keyup change', function (e) { highLightSaveButtons(); });

Assuming its possible to have 2 change events on the select list, its would assume that setting keypress & keyup events may be breaking something?

Any ideas?

ANOTHER UPDATE:

The select element looks fine and all works if I remove the additional 'change' binding. when the 'change' binding is added that event fires once and the hard-wired 'onchange' is fired twice.

If both events are bound via jQuery all works ok, it seems that mixing hard-wired onchange and jquery bound change events cannot be mixed? I guess this answers my question but seems like an issue with IE and binding these events with jquery.

解决方案

I agree with your assessment. I've updated my small example at http://gutfullofbeer.net/onchange.html to include a jQuery handler in addition to the DOM 0 handler (the one set with the "onchange" attribute). It appears to be a jQuery bug, or at least a failure of jQuery to deal with the IE weirdness.

I logged jQuery ticket 6593.

这篇关于Html Select List:为什么onchange会被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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