1
$("#post_text").keydown(function(e){
// Enter was pressed without shift key
if (e.keyCode == 13) {
    // prevent default behavior
    e.preventDefault();
} 
if (e.keyCode == 13 && !e.shiftKey) {
alert('test');
}

});

我需要类似这样的功能,我需要使用 Enter 键发布,但使用 shift 和 enter 键创建新行。就像 facebook 消息系统一样。

4

1 回答 1

4

您需要考虑这shiftKey两种情况下的价值。

$("#post_text").keydown(function(e){
    if (e.which === 13) {
        // Enter was pressed
        if (e.shiftKey) {
            // With shift
        }
        else {
            // Without shift
        }
    }
});

还要注意使用which,不是keyCode。jQuery 标准化事件对象,确保which在没有设置它的浏览器上设置。

于 2013-07-13T09:14:29.833 回答