Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap-tagsinput form submited on press enter key

in bootstrap-tagsinput on press enter key for next tag form is submitted! what is the solution?

$("#inputID").tagsinput();
like image 487
Mojnegar it Avatar asked Jun 22 '16 16:06

Mojnegar it


People also ask

How do I stop form submission on Enter key?

Disallow enter key anywhere on("keydown", "form", function(event) { return event. key != "Enter"; }); This will cause that every key press inside the form will be checked on the key .


5 Answers

Go to tagsinput.js

Find line:

self.$container.on('keypress', 'input', $.proxy(function(event) {

Before the end of this function add following:

if (event.which == 13) {
    return false; // dont submit form !!
}
like image 156
Martin Zvarík Avatar answered Sep 30 '22 08:09

Martin Zvarík


This work for me

$('.bootstrap-tagsinput input[type=text]').on('keydown', function (e) {
   if ( event.which == 13 ) {
           $(this).blur();
           $(this).focus();
           return false;
      }
 });
like image 34
msayubi76 Avatar answered Sep 30 '22 07:09

msayubi76


First, you'll want to map 'Enter' to your tagsinput's confirmkeys option, then you'll need to call preventDefault() on your enter key to prevent the form from being submitted. In my solution, it only prevents submission by enter key while the user is focused in the tags input field.

To add a bit of redundancy, I also re-map the enter key to a comma in the input field, just to be sure.

  $('#tags-input').tagsinput({
    confirmKeys: [13, 188]
  });

  $('#tags-input input').on('keypress', function(e){
    if (e.keyCode == 13){
      e.keyCode = 188;
      e.preventDefault();
    };
  });

Just also an FYI, Enter key is mapped as 13, and comma is 188 in JS.

like image 40
Shelby S Avatar answered Sep 30 '22 06:09

Shelby S


Try this:

$('.bootstrap-tagsinput input').keydown(function( event ) {
    if ( event.which == 13 ) {
        $(this).blur();
        $(this).focus();
        return false;
    }
})
like image 31
bharat Avatar answered Sep 30 '22 08:09

bharat


Enter key is register for postback form . that's why a case has been happened that when you hit 'enter' key it trigger the 'postback' event so the form goes to submitted . solution : on page load reserve the enter key for tag input :

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))

if using asp.net mvc framework this is a simple example .

like image 33
Noni Avatar answered Sep 30 '22 06:09

Noni