【发布时间】:2014-01-24 09:56:42
【问题描述】:
我有一个表单,我在输入文本框中输入一个数字并按 ENTER,然后使用 jQuery 将值附加到文本区域。这一切都很好。
我遇到的问题是,如果我添加一个提交按钮来提交表单,只要我按下 ENTER,它就会提交表单。
我希望它做的不是在按 Enter 时提交表单,而是仅在单击提交按钮时提交表单。
我尝试使用 preventDefault() 并返回 false,这将在按 ENTER 时停止提交表单,但如果我在提交按钮上添加点击事件来提交表单,它什么也不做。我在提交之前在点击功能中放置了一个警报,它会触发但表单没有提交
<form id="toteform" method="post" action="blah.php">
<input type="text" name="bin" id="bin" maxlength="4" autocomplete="off" />
<input type="text" name="totes" id="tote" maxlength="4" autocomplete="off" />
<input type="button" name="submit" class="submit" id="submit" value="Submit" />
</form>
jQuery
$("#submit").click(function() {
$('#toteform').submit();
});
$('#bin').focus();
$('#bin').keypress(function(e) {
if(e.which == 13) {
$('#tote').focus();
}
});
$('#tote').keypress(function(e) {
if(e.which == 13) {
// more code here to do other things
【问题讨论】:
标签: jquery forms submit form-submit keypress