jQuery自动完成:点击返回提交表单 [英] jQuery Autocomplete: Hitting return submits the form
问题描述
我正在使用jQuery Autocomplete插件。
I am using the jQuery Autocomplete plugin.
// I'm not sure if there's a better way to do this
var base_url = window.location.href.slice(0, window.location.href.indexOf('/bar'));
$('.foo-widget').find('input:first-child').autocomplete(base_url + "/api/baz?format=newline");
加价:
<ul class="foo-widget">
<li>
<input value="" autocomplete="off" class="ac_input"><input value="" autocomplete="off" class="ac_input"><button>Add</button>
</li>
</ul>
这很好用。但是,当我点击输入从自动填充结果中选择一个项目时,表单会提交。为什么会发生这种情况?
This works fine. However, when I hit "enter" to choose an item from the autocomplete results, the form submits. Why might this be happening?
我查看了 jquery.autocomplete.js 并看到以下内容正在执行:
I looked into jquery.autocomplete.js and saw that the following is being executed:
case KEY.RETURN:
if( selectCurrent() ) {
// stop default to prevent a form submit, Opera needs special handling
event.preventDefault();
blockSubmit = true;
return false;
}
break;
不应该 event.preventDefault()
和 blockSubmit = true
停止提交表单?我在页面上其他地方的JS代码是否可能会干扰?
Shouldn't event.preventDefault()
and blockSubmit = true
stop the form from submitting? Is it possible that JS code I have elsewhere on the page is interfering?
推荐答案
我建议在自动完成时添加按键事件元素并从中返回false。它不会影响您以后提交表格其余部分的能力。
I would recommend adding an keypress event on the Autocomplete element and return false from that. It will not impact your ability to submit the rest of the form at a later time.
这篇关于jQuery自动完成:点击返回提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!