简体   繁体   English

<select>上的jQuery更改事件未在IE中触发

[英]jQuery change event on <select> not firing in IE

I've got a page with a variable number of <select> elements (which explains why I'm using event delegation here). 我有一个页面,其中包含可变数量的<select>元素(这解释了我在这里使用事件委托的原因)。 When the user changes the selected option, I want to hide/show different content areas on the page. 当用户更改所选选项时,我想在页面上隐藏/显示不同的内容区域。 Here's the code I have: 这是我的代码:

$(document).ready(function() {
  $('#container').change(function(e) {
    var changed = $(e.target);

    if (changed.is('select[name="mySelectName"]')) {
      // Test the selected option and hide/show different content areas.
    }
  });
});

This works in Firefox and Safari, but in IE the change event doesn't fire. 这适用于Firefox和Safari,但在IE中,更改事件不会触发。 Anyone know why? 谁知道为什么? Thanks! 谢谢!

The change event does not bubble in IE (See here and here ). change事件不会在IE中冒泡(请参阅此处此处 )。 You cannot use event delegation in tandem with it. 您不能将事件委托与其一起使用。

In fact, it is because of this IE bug that jQuery live had to officially exclude change from the list of supported events (FYI the DOM spec states change should bubble ) . 实际上,正是由于这个IE bug,jQuery live必须正式排除 支持事件列表中的 change (FYI DOM规范指出 change 应该冒泡 [ 1 ] [ 1 ]

With respect to your question, you can bind directly to each select: 关于您的问题,您可以直接绑定到每个选择:

$('#container select').change(/*...*/)

If you really want event delegation you might find some success trying what this person did and bind to click in IE only, which does bubble: 如果你真的想要事件委托你可能会发现尝试这个人所做的事情并绑定到IE中的click只会有一些成功,这冒泡:

$('#container').bind($.browser.msie ? 'click' : 'change', function(event) {
    /* test event.type and event.target 
     * to capture only select control changes
     */
})

But this browser detection feels really wrong. 但是这种浏览器检测感觉非常错误。 I'd really try working with the former example (binding directly to the drop downs). 我真的尝试使用前一个例子(直接绑定到dropdown)。 Unless you have hundreds of <select> boxes, event delegation wouldn't buy you much here anyway. 除非你有数百个<select>框,否则事件委托在这里不会给你带来太多帮助。


[ 1 ] Note: jQuery >= 1.4 now simulates a bubbling change event in IE via live() / on() . [ 1 ]注意:jQuery> = 1.4现在通过live() / on()模拟IE中的冒泡change事件。

Idea that might help: 可能有所帮助的想法:

$(document).ready(function() {
  $('#container select[name="mySelectName"]').change(function(e) {
    var s = $(e.target);
    if (s.val()=='1') //hide/show something;
  });
});

If you are using AJAX, try live() function: 如果您使用的是AJAX,请尝试使用live()函数:

 $(document).ready(function() {
       $('#container select[name="mySelectName"]').live('change', function(e) {
        var s = $(e.target);
        if (s.val()=='1') //hide/show something;
      });
    });

If I recall correctly you will need to call blur() to have jQuery invoke change() on IE machines. 如果我没记错的话,你需要调用blur()让jQuery在IE机器上调用change()。 Try something like: 尝试类似的东西:

$("select[name=mySelectName]").click(function() {
    $(this).blur();
});

使用jquery 1.4.4(我认为1.4.3)现在似乎都很好......改变事件在我的有限测试中始终如一。

onchange=doAction will work in IE and Firefox, but its not supported in Chrome. onchange=doAction可以在IE和Firefox中使用,但在Chrome中不支持。

You need to use jQuery's .change event to handle this. 你需要使用jQuery的.change事件来处理这个问题。

Add this lines to your page head, Sit back and relax! 将这些行添加到页面头部,坐下来放松一下! :) :)

$(document).ready(function(){$('select').bind('onChange',function(){$(this).blur()});});

IE requires change event to be placed inside document ready. IE要求将更改事件放在文档准备就绪中。 This seems to bind the change event to the associated element. 这似乎将change事件绑定到关联元素。 Hope it helps. 希望能帮助到你。

I'm trying to understand why you need to double check the name of the select after receiving an event to it. 我试图理解为什么在收到事件后需要仔细检查选择的名称。

Do you by any chance have multiple elements with the same id ? 您是否有任何机会拥有多个具有相同ID的元素?

Did you actually mean to say "#container select" instead of "#container" ? 你真的想说“#container select”而不是“#container”吗?

I'm simply building upon the example set by "Crescent Flesh" for a cross-platform solution that will survive even if loading this SELECT inside #container via an AJAX call. 我只是在“Crescent Flesh”设置的示例基础上构建了一个跨平台的解决方案,即使通过AJAX调用将此SELECT加载到#container中也能生存下来。

$('#container').bind($.browser.msie ? 'click' : 'change', function(event) {
  if ((event.type == 'click') || (event.type == 'change')) {
    if (event.target.toString().indexOf('Select') != -1) {
      var sWhich = $('#container SELECT').val();
      handleSelectionChange(sWhich);
    }
  }
});

Now you simply build the handleSelectionChange() function, renaming it whatever you want. 现在,您只需构建handleSelectionChange()函数,并根据需要重命名它。

:D:D Wow, I was finding solution... Why think so complicated? :D:D哇,我找到了解决方案......为什么这么复杂? Simply: 只是:
<select onchange="doAction">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM