(回车键&&(Shift键)在j查询中运行多次。需要帮助。提前谢谢。
<textarea id = 'text' rows='4' cols='50' style = 'width: 50%;height:15%;' onkeypress = "go('6','5')" placeholder = 'Enter Post....'></textarea>;
function go(a,b){
$(document).ready(function() {
$('#text').keypress(function (e) {
if (e.keyCode == 13 && e.shiftKey) {
alert(a);
} else if (e.keyCode == 13) {
alert(b);
}
});
});
}
因为你已经应用了内联
onkeypress
事件,你不需要在你的go
函数中再次应用.keypress()
事件。
function go(a,b){
if (e.keyCode == 13 && e.shiftKey) {
alert(a);
} else if (e.keyCode == 13) {
alert(b);
}
}
这是因为当.keypress()
事件已经在内联onkeypress
中应用时,您已经嵌套了keypress
调用,所以最好的解决方案是删除内联onkeypress
事件,您可以使用.keypress()
来实现您的目标:
$('#text').keypress(function (e) {
if (e.keyCode == 13 && e.shiftKey) {
alert(a);
} else if (e.keyCode == 13) {
alert(b);
}
});
<<p> 小提琴演示/strong> 看起来应该是这样的:
<textarea id = 'text' rows='4' cols='50' style = 'width: 50%;height:15%;' placeholder = 'Enter Post....'></textarea>;
$(document).ready(function() {
$('#text').keypress(function (e) {
if (e.keyCode == 13 && e.shiftKey) {
alert(a);
} else if (e.keyCode == 13) {
alert(b);
}
});
});
ready()调用将为文本区域设置一个按键监听器。无需在HTML中单独定义onkeypress属性。
或者,如果您想保留onkeypress属性而不是使用$。按下键,它看起来像:
<textarea id = 'text' rows='4' cols='50' style = 'width: 50%;height:15%;' onkeypress = "go('6','5')" placeholder = 'Enter Post....'></textarea>;
function go(a,b){
if (e.keyCode == 13 && e.shiftKey) {
alert(a);
} else if (e.keyCode == 13) {
alert(b);
}
}
$.ready()函数在文档加载完成时运行。这里不需要这样做,因为您只定义了一个函数(不需要等待文档准备好定义函数)。$.keypress()函数创建一个按键监听器。这里不需要这样做,因为您已经使用onkeypress属性添加了一个侦听器。所以,你剩下的就是onkeypress代码调用的函数。