She*_*fie 3 html javascript forms jquery
具体来说,我不想使用event.preventDefault()按下 Enter 键时已经推荐的方法,因为这至少会破坏 Internet Explorer 中数据列表的功能。
我尝试过类似的事情
$("#form").submit(function(event){
console.log(event);
return false; // Allows me to read the console before the form submits!
});
Run Code Online (Sandbox Code Playgroud)
比较事件之间的差异(单击提交按钮与按回车键),似乎没有任何明显的差异。我本以为后者会有一个event.keyCode,但事实并非如此。
我能想到的一个冗长的解决方法似乎是删除我的提交按钮,并将其替换为常规按钮,其单击必然会提交表单。但那样的话,我将不得不捕获输入中的回车键按下并处理从头开始提交表单。丑陋,但可行。再说一次,如果我能帮助的话,我不想应用任何s。preventDefault()
这是一种 hacky 解决方案,但您可以将事件处理程序绑定到按钮click上的a submit,而submit不是#form. 然后您可以检查是否存在与事件关联的非零坐标。如果是,submit则单击该按钮,如果否,则按下回车键。例如
$("input[type='submit']").click(function(event){
if (event.screenX === 0) alert("You hit the enter key!");
else alert("You clicked the submit button!");
return false; // Allows me to read the console before the form submits!
});
Run Code Online (Sandbox Code Playgroud)