简体   繁体   English

jQuery-更改时选择事件导致IE10和FF中的选择框立即再次关闭

[英]jQuery - select on change event causes select-box to instantly close again in IE10 and FF

i am having a very strange and annoying problem with binding jquery "on change" events to <select> - boxes in IE10 and (various) firefox-version. 我有一个非常奇怪和烦人的问题,将jQuery“更改时”事件绑定到<select>事件-IE10和(各种)firefox版本中的框。
this problem does not occur in Chrome or IE11. 在Chrome或IE11中不会发生此问题。

the dropdown-menu which is opening after clicking a select-box is instantly closed again, but only if there is an "on change" event bound to it. 单击选择框后打开的下拉菜单会立即再次关闭,但前提是绑定了“更改时”事件。
i am using quite a standard jquery routine here: 我在这里使用相当标准的jquery例程:

$(document).on("change", "#kp_gebdat_y, #kp_gebdat_m, #kp_gebdat_d", function () {

    var year = $("#kp_gebdat_y").val();
    var month = $("#kp_gebdat_m").val();
    var day = $("#kp_gebdat_d").val();

    $("#kp_gebdat").val(year + "-" + month + "-" + day);
});

I already tried to mess around with preventDefault() but that doesn't change anyhting. 我已经尝试过使用preventDefault()但这并没有改变。 it seems like the first dropdown is "focused" after any of the dropboxes are clicked in IE10. 在IE10中单击任何下拉框后,第一个下拉菜单似乎是“集中的”。

here is a JSFiddle that reproduces the problem in IE10 and FF as well: http://jsfiddle.net/northkildonan/7zyw2nff/1/ 这是一个JSFiddle,它也再现了IE10和FF中的问题: http : //jsfiddle.net/northkildonan/7zyw2nff/1/

Any help would be appreciated! 任何帮助,将不胜感激!

As mentioned in the comment above, the problem is not jQuery or the JavaScript event handler, it is the markup itself. 如上面的注释中所述,问题不是jQuery或JavaScript事件处理程序,而是标记本身。 The select fields are nested inside a label tag: 选择字段嵌套在标签标签内:

<label>
    <select type="text">
        <option value="0">Tag</option>
        [...]   
    </select>
    <select type="text">
        <option value="0">Monat</option>
        [...]   
    </select>
    <select type="text">
        <option value="0">Jahr</option>
        [...]   
    </select>   
</label>

And this is what causes the error: In case you focus the second or third select field, the event is delegated to the label, which is (in FF) bind to the first form field child - your first select field and focussing it - and that focus causes the immediate blur of second + third select field inside the same label. 这就是导致错误的原因:如果您将焦点放在第二个或第三个选择字段上,则该事件将委派给标签,该标签(在FF中)绑定到第一个表单字段子对象-您的第一个选择字段并对其进行聚焦-并且该焦点会导致同一标签内第二个+第三个选择字段立即模糊。 So you can either remove the label completely or nest each select in an own label: 因此,您可以完全删除标签,也可以将每个选择嵌套在自己的标签中:

<label>
    <select type="text">
        <option value="0">Tag</option>
        [...]   
    </select>
</label>
<label>
   [...]
</label>

I think at the end FF behaviour is correct, because an label should be related to one element. 我认为最后FF行为是正确的,因为标签应与一个元素相关。

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

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