how prevent submit on press enter in twitter bootstrap typeahead plugin

这一生的挚爱 提交于 2019-12-03 06:21:55

You can target that specific input by adding an ID to it and simply removing the keydown event for enter like so:

JS

$(document).ready(function() {
  $('form input').keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});

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:

$(".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);
});
adrien

According to the documentation, you can set the option confirmKeys to something else than return (13) :

$('input').tagsinput({
   confirmKeys: [188, 32, 39],
});

However, a smarter way to go about this is to prevent sending the form on enter.

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.

$('#my_submit_button').click(function(){
    $(this).parents('form').submit();
});
Zulqarnain

I have same problem in my project using CodeIgniter and bootstrap here i found the solution and it works.

Just add onsubmit="return false;" in the form, like this:

<form id="frmcrud" method="post" onsubmit="return false;">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!