atp*_*atp 14 jquery event-handling
我有:
$('#myTextArea').keyup(function(e) {
if(e.keyCode == 13) {
e.preventDefault(); // Makes no difference
$(this).parent().submit(); // Submit form it belongs to
}
});
Run Code Online (Sandbox Code Playgroud)
如何阻止在表单提交之前显示的换行符?preventDefault()并且stopPropagation不工作.我可以手动重置字段文本,但是......
Gab*_*oli 27
发生这种情况是因为keyup在文本区域中插入文本后调用.
要在输入之前捕获密钥,您需要监听keydown事件.
因此,只要改变keyup对keydown你应该是好去.
$('#myTextArea').keydown(function(e) {
Run Code Online (Sandbox Code Playgroud)