2

在 Chrome 和 FF 中,当在文本框中按 Enter 时会触发更改,而不是在 IE 中,所以我试图让它做同样的事情

这就是我目前所拥有的:http: //jsfiddle.net/nV4SA/8/

//fix for IE to trigger change on enter
    $("input[type=text]")
        .on("change", function(e) {
            $.data(this, "value", this.value);
        }).on("keyup", function(e) {
            if (e.which === 13 && this.value != $.data(this, "value")) {
                $(this).change();
            }
        });

此代码的唯一缺点是在 IE 用户点击输入时会发生更改,但是当文本框失去焦点时更改会再次发生

4

1 回答 1

2

如果您仅在 IE中触发change,它似乎不会触发它。blur用于$.support测试 IE。

/* false in IE.. see jQuery APU */
var changeBubble=$.support.changeBubbles;

$("input[type=text]").on("change", function(e) {
    $.data(this, "value", this.value);
}).on("keyup", function(e) {
    if (e.which === 13 && this.value != $.data(this, "value") && !changeBubble) {
        e.preventDefault();
        $(this).trigger('blur');
    }
})

演示:http: //jsfiddle.net/nV4SA/10/

$.supportAPI 文档:http ://api.jquery.com/jQuery.support/

编辑:请注意,这$.support.changeBubbles在 IE9 中是正确的,但不是 IE <9。解决方案可能不是万无一失的。尽管不赞成您可能需要使用浏览器嗅探或尝试$.support在所有版本的 IE 中查找其他支持属性

于 2012-11-03T15:05:27.347 回答