我需要使用回车键发布,但新行有 shift 和回车键

i need to post with enter key but have shift & enter key for new line

本文关键字:回车 新行 shift      更新时间:2023-09-26
$("#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消息系统一样。

在这两种情况下,您都需要考虑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