Javascript jQuery自动完成:点击return提交表单
我正在使用jQuery自动完成插件Javascript jQuery自动完成:点击return提交表单,javascript,jquery,jquery-autocomplete,Javascript,Jquery,Jquery Autocomplete,我正在使用jQuery自动完成插件 // 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");
// 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>
event.preventDefault()
和blockSubmit=true
是否应该阻止表单提交?我在页面其他地方的JS代码是否可能会产生干扰?您将希望通过连接form.submit:$(“form”).submit(function()的事件处理程序来禁用按enter键提交{
返回false;
}); 我建议在Autocomplete元素上添加一个keypress事件,并从中返回false。它不会影响您以后提交表单其余部分的能力。在到达preventDefault()代码之前,可能会出现另一个javascript错误。Firebug是否显示任何错误?Return false阻止提交。该页面中有多少表单?@aSeptik只有一个表单@Jared Farrish Firebug没有显示任何错误。Rosarch,如果找到解决方案,请发布代码片段。但我不希望总是禁用表单提交-我只希望在用户在自动完成结果时单击“返回”时禁用它。
case KEY.RETURN:
if( selectCurrent() ) {
// stop default to prevent a form submit, Opera needs special handling
event.preventDefault();
blockSubmit = true;
return false;
}
break;