Why do my jQuery checkbox.change events only fire on leavefocus in IE but they happen onclick in FF?

孤街浪徒 提交于 2019-12-07 01:15:11

问题


    $(".feature").change(function(){
        getProductSelections();
    });

ARRRGHH!!


回答1:


Web Bug Track:

The onchange event can be attached (inline or as an event handler) to any form element. It fires whenever the value of the form field changes. Unfortunately, the behavior is a bit strange in IE, in that for a checkbox, or a radio button field, the event doesn't fire when it is supposed to (right when you click the option you want to choose), but instead it only fires, when you click elsewhere on the page/form, or if you explicitly call blur(); on the field.

And the work around suggested is:

<input type="radio" name="foo" value="Green" onclick="alert(this.value);"/>Green
<input type="radio" name="foo" value="Blue" onclick="alert(this.value);"/>Blue



回答2:


Problem with Lucas's solution is that this will trigger even if you click the same option again.

The solution I found to work was to add onclick="this.blur();" to the radio button tag which forces it to trigger the onchange only if it has changed.

<input type="radio" name="foo" value="Green" onclick="alert(this.value);" onclick="this.onBlur();" />Green
<input type="radio" name="foo" value="Red" onclick="alert(this.value);" onclick="this.onBlur();" />Red

Hope this helps.



来源:https://stackoverflow.com/questions/1501743/why-do-my-jquery-checkbox-change-events-only-fire-on-leavefocus-in-ie-but-they-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!