试图让jQuery检测输入输入,但是检测到空格和其他键,则不会检测到输入。下面有什么问题:
$("#entersomething").keyup(function(e) { alert("up"); var code = (e.keyCode ? e.keyCode : e.which); if (code==13) { e.preventDefault(); } if (code == 32 || code == 13 || code == 188 || code == 186) { $("#displaysomething").html($(this).val()); }); <input id="entersomething" /> <div id="displaysomething"></div>
解决方法
JavaScript / jQuery
$("#entersomething").keyup(function(e){ var code = e.which; // recommended to use e.which,it's normalized across browsers if(code==13)e.preventDefault(); if(code==32||code==13||code==188||code==186){ $("#displaysomething").html($(this).val()); } // missing closing if brace });
HTML
<input id="entersomething" type="text" /> <!-- put a type attribute in --> <div id="displaysomething"></div>