简体   繁体   English

为什么它在IE 6-8中不起作用(捕获jQuery输入)

[英]Why it doesn't work in IE 6-8 (catching Enter with jQuery)

This code doesn't work in IE 6-8. 此代码在IE 6-8中不起作用。 Why? 为什么?

$(".put").keydown(function (e) {
    if (e.keyCode == 13) {
        if ($(this).is(":focus")) {
            $(this).submit().select();
        }
        return false;
    }
});

Here's the full page https://rawgithub.com/ruslankh/Kurchatovy/master/index.html 这是整页https://rawgithub.com/ruslankh/Kurchatovy/master/index.html

Thank you :) 谢谢 :)

Try using e.which() to support all the browsers 尝试使用e.which()支持所有浏览器

 var code = e.which;

 if(code == 13) { //Enter keycode
        if ($(this).is(":focus")) {
            $(this).submit().select();
        }
        return false;
 }

jQuery will normalize the keyCode into e.which - so it should be safe to use just e.which. jQuery会将keyCode标准化e.which-因此仅使用e.which应该是安全的。

I'm however unsure about the following code - I'm surprised it works in other browsers than IE6-8. 但是,我不确定以下代码-令我惊讶的是,它在IE6-8之外的其他浏览器中也可以使用。

$(this).submit().select()

In the context above $(this) is the input-element. $(this)以上的上下文中, 输入元素。 The submit() function should be called on the form element. Submit()函数应在form元素上调用。 The input element can not be submitted by itself, the onsubmit event exists only on the form element . 输入元素本身不能提交, onsubmit事件仅存在于form元素上

As Brewal mentions in his comment, you should find the wrapping form element and submit() the form. 正如Brewal在他的评论中提到的那样,您应该找到包装表单元素并提交()表单。 Without a form-element your input element has nowhere to post its content. 没有格式元素,您的输入元素将无处发布其内容。

certain browsers do not support e.keycode see this example 某些浏览器不支持e.keycode参见此示例

    var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
    alert('You pressed a "enter" key in textbox');  
}

also if you using or have recently switched to a newer version of jquery ( 2.0+) IE8 and below are no longer supported 另外,如果您使用或最近切换到新版本的jquery(2.0+)IE8及更低版本,则不再支持

also using $(this).submit() inside of your function, is telling the input to submit itself, but inputs do not submit, the form submits. 同样在函数内部使用$(this).submit()告诉输入要提交自己,但是输入不提交,表单会提交。

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

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