jQuery 如何防止在 twitter bootstrap typeahead 插件中按回车提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13551668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how prevent submit on press enter in twitter bootstrap typeahead plugin
提问by paganotti
I have a input text and I apply it typeahead plugin for suggesting items, but when I press enter key on input text it submit form.
我有一个输入文本,我应用它预先输入插件来建议项目,但是当我在输入文本上按 Enter 键时,它会提交表单。
How can I prevent form submit using twitter bootstrap typeahead plugin?
如何使用 twitter bootstrap typeahead 插件阻止表单提交?
回答by Andres Ilich
You can target that specific input by adding an ID to it and simply removing the keydown event for enter like so:
您可以通过向其添加 ID 并简单地删除输入的 keydown 事件来定位该特定输入,如下所示:
JS
JS
$(document).ready(function() {
$('form input').keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
回答by Tarek
The first answer didn't worked for me, maybe because of updating the typeahead plugin, but I did passed the problem by using the 'alternative' input created by typehead:
第一个答案对我不起作用,可能是因为更新了 typeahead 插件,但我确实通过使用 typehead 创建的“替代”输入解决了这个问题:
$(".twitter-typeahead > input").focus(function(){
//disable btn when focusing the input
$("#my_submit_button").prop('disabled', true);
}).blur(function(){
//enable btn when bluring the input
$("#my_submit_button").prop('disabled', false);
});
回答by adrien
According to the documentation, you can set the option confirmKeys
to something else than return (13) :
根据文档,您可以将选项设置为confirmKeys
return (13) 以外的其他选项:
$('input').tagsinput({
confirmKeys: [188, 32, 39],
});
However, a smarter way to go about this is to prevent sending the form on enter.
但是,解决此问题的更明智的方法是防止在 enter 时发送表单。
回答by Jim Ready
The best way I've found to approach this is not to use a submit input. Use a button input instead and add some js to submit the form when it's clicked.
我发现解决这个问题的最好方法是不使用提交输入。改用按钮输入并添加一些 js 以在单击时提交表单。
$('#my_submit_button').click(function(){
$(this).parents('form').submit();
});
回答by Zulqarnain
I have same problem in my project using CodeIgniter and bootstrap here i found the solution and it works.
我在使用 CodeIgniter 和 bootstrap 的项目中遇到了同样的问题,我在这里找到了解决方案并且它有效。
Just add onsubmit="return false;"
in the form, like this:
只需onsubmit="return false;"
在表单中添加,如下所示:
<form id="frmcrud" method="post" onsubmit="return false;">