本文介绍了jQuery选择更改/单击问题-更改在IE中不起作用,单击在Chrome中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我在绑定到下拉选择列表中的更改时遇到了问题-似乎更改"不适用于IE(7或8),因此当我尝试替代方法并使用"click"事件,它在IE中有效,但在Chrome中不起作用!我在这里错过明显的东西吗?

As the title says, I have a problem with binding to a change in a dropdown select list - it seems that "change" doesn't work with IE(7 or 8), so when I try the alternative and use a "click" event, it works in IE but doesn't work in Chrome! Am I missing something obvious here?

这是我的代码:

//event handler for showing hidden form elements (also ensures only relevant hidden els shown)
        //IE needs click event instead of change
        $('.select_change').live("change", function(){
            //check if value is other
            if ($(this).val() == 'other') 
                $(this).parent().find(".hidden").show();
            //if user changes select value from other then hide input
            if ($(this).val() != 'other') 
                $(this).parent().find(".hidden").hide();
            return false;
        });

下拉HTML如下:

<select id="title" name="title" class="validate[required,funcCall[validateNotDefault]] select_change" >
<option value="default" selected="selected">Please choose from options</option>
<option value="yellow">Yellow</option>
<option value="black">Black</option>
<option value="chocoloate">Chocolate</option>

<option value="other">Other</option>
</select>

推荐答案

change在IE中仍然不能完全正确冒泡,尽管如此,您仍可以在两种情况下使用此方法:

change still doesn't bubble completely correctly in IE, you can do this to work in both cases though:

$('.select_change').live("change click", function(){
   $(this).parent().find(".hidden").toggle($(this).val() == 'other');
});

它仍然使用.live(),仅对两个事件都响应...因为您执行的操作不会因触发一两次而受到伤害,因此在这种情况下可以这样做上面的内容只是使用 .toggle(bool) 来简化一点.

This still uses .live(), just responds to both events...since what you're doing isn't harmed by firing once or twice, it's fine to do so in this case. The above is just a bit shorter using .toggle(bool) to simplify your logic a bit.

这篇关于jQuery选择更改/单击问题-更改在IE中不起作用,单击在Chrome中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 22:34