Do not send form with the Jquery animation of the search button

0

I have this code that does the button animation fetch in Jquery, the problem is that my form does not send the typed data when you click the submit button with this animation, how should I do?

HTML

<form class="navbar-form" role="search" action="">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Procurar">
                        <span class="input-group-btn">
                            <button type="reset" class="btn btn-default">
                                <span class="glyphicon glyphicon-remove">
                                    <span class="sr-only">Close</span>
                                </span>
                            </button>

                            <button type="submit" class="btn btn-default">
                                <span class="fas fa-search procurar">
                                    <span class="sr-only">Search</span>
                                </span>
                            </button>
                        </span>
                    </div>
                </form>

CSS

.navbar-collapse {
        position: relative;
        padding-top: 30px !important;
        max-height: 270px;
    }
    .navbar-collapse form[role="search"] {
    position: absolute;
    top: 35px;
    right: 0px;
    width: 100%;
    padding: 0px;
    margin: 0px;
    z-index: 0;
}
.input_procurar {
    width: 30%;
    float: right;

}

.procurar {
    font-size: 18px;
}
    .navbar-collapse form[role="search"] button,
    .navbar-collapse form[role="search"] input {
        padding: 8px 12px;
        border-radius: 0px;
        border-width: 0px;
        color: rgb(119, 119, 119);
        background-color: rgb(248, 248, 248);
        border-color: rgb(231, 231, 231);
        box-shadow: none;
        outline: none;
    }
    .navbar-collapse form[role="search"] input {
    padding: 16px 12px;
    font-size: 19px !important;
    font-style: normal;
    color: rgb(160, 160, 160);
    box-shadow: none;
}
    .navbar-collapse form[role="search"] button[type="reset"] {
        display: none;
    }

    @media (min-width: 768px) {
        .navbar-collapse {
            padding-top: 0px !important;
            padding-right: 38px !important;
        }
        .navbar-collapse form[role="search"] {
            width: 38px;
        }
        .navbar-collapse form[role="search"] button,
        .navbar-collapse form[role="search"] input {
            padding: 15px 12px;
        }
        .navbar-collapse form[role="search"] input {
            font-size: 18pt;
            opacity: 0;
            display: none;            
            height: 50px;
        }
    .navbar-collapse form[role="search"].active {
    width: 40%;
}
        .navbar-collapse form[role="search"].active button,
        .navbar-collapse form[role="search"].active input {
            display: table-cell;
            opacity: 1;
        }
        .navbar-collapse form[role="search"].active input {
            width: 100%;
        }
    }

JS

 $(function () {
        // Remove Search if user Resets Form or hits Escape!
        $('body, .navbar-collapse form[role="search"] button[type="reset"]').on('click keyup', function(event) {
            console.log(event.currentTarget);
            if (event.which == 27 && $('.navbar-collapse .input_procurar form[role="search"]').hasClass('active') ||
                $(event.currentTarget).attr('type') == 'reset') {
                closeSearch();
            }
        });

        function closeSearch() {
            var $form = $('.navbar-collapse form[role="search"].active')
            $form.find('input').val('');
            $form.removeClass('active');
        }

        // Show Search if form is not active // event.preventDefault() is important, this prevents the form from submitting
        $(document).on('click', '.navbar-collapse form[role="search"]:not(.active) button[type="submit"]', function(event) {
            event.preventDefault();
            var $form = $(this).closest('form'),
                $input = $form.find('input');
            $form.addClass('active');
            $input.focus();

        });
        // ONLY FOR DEMO // Please use $('form').submit(function(event)) to track from submission
        // if your form is ajax remember to call 'closeSearch()' to close the search container
        $(document).on('click', '.navbar-collapse form[role="search"].active button[type="submit"]', function(event) {
            event.preventDefault();
            var $form = $(this).closest('form'),
                $input = $form.find('input');
            $('#showSearchTerm').text($input.val());
            closeSearch()
        });
    });

Libraries

<!-- Latest compiled and minified JavaScript -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<!-- Plugin JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>

<!--Customfontsforthistemplate--><linkrel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">
    
asked by anonymous 02.10.2018 / 15:56

1 answer

1

The problem was in the click function, it references a button within a form with the active class

 $(function () {
        // Remove Search if user Resets Form or hits Escape!
        $('body, .navbar-collapse form[role="search"] button[type="reset"]').on('click keyup', function(event) {
            console.log(event.currentTarget);
            if (event.which == 27 && $('.navbar-collapse .input_procurar form[role="search"]').hasClass('active') ||
                $(event.currentTarget).attr('type') == 'reset') {
                closeSearch();
            }
        });

        function closeSearch() {
            var $form = $('.navbar-collapse form[role="search"].active')
            $form.find('input').val('');
            $form.removeClass('active');
        }

        // Show Search if form is not active // event.preventDefault() is important, this prevents the form from submitting
        $(document).on('click', '.navbar-collapse form[role="search"]:not(.active) button[type="submit"]', function(event) {
            event.preventDefault();
            var $form = $(this).closest('form'),
                $input = $form.find('input');
            $form.addClass('active');
            $input.focus();

        });
        // ONLY FOR DEMO // Please use $('form').submit(function(event)) to track from submission
        // if your form is ajax remember to call 'closeSearch()' to close the search container
        $(document).on('click', 'button[type="submit"]', function(event) {
            event.preventDefault();
            var $form = $(this).closest('form'),
                $input = $form.find('input');
                alert('valor do input: '+ $form.find('input').val());
                console.log($input.val());
            $('#showSearchTerm').text($input.val());
            closeSearch()
        });
    });
.navbar-collapse {
        position: relative;
        padding-top: 30px !important;
        max-height: 270px;
    }
    .navbar-collapse form[role="search"] {
    position: absolute;
    top: 35px;
    right: 0px;
    width: 100%;
    padding: 0px;
    margin: 0px;
    z-index: 0;
}
.input_procurar {
    width: 30%;
    float: right;

}

.procurar {
    font-size: 18px;
}
    .navbar-collapse form[role="search"] button,
    .navbar-collapse form[role="search"] input {
        padding: 8px 12px;
        border-radius: 0px;
        border-width: 0px;
        color: rgb(119, 119, 119);
        background-color: rgb(248, 248, 248);
        border-color: rgb(231, 231, 231);
        box-shadow: none;
        outline: none;
    }
    .navbar-collapse form[role="search"] input {
    padding: 16px 12px;
    font-size: 19px !important;
    font-style: normal;
    color: rgb(160, 160, 160);
    box-shadow: none;
}
    .navbar-collapse form[role="search"] button[type="reset"] {
        display: none;
    }

    @media (min-width: 768px) {
        .navbar-collapse {
            padding-top: 0px !important;
            padding-right: 38px !important;
        }
        .navbar-collapse form[role="search"] {
            width: 38px;
        }
        .navbar-collapse form[role="search"] button,
        .navbar-collapse form[role="search"] input {
            padding: 15px 12px;
        }
        .navbar-collapse form[role="search"] input {
            font-size: 18pt;
            opacity: 0;
            display: none;            
            height: 50px;
        }
    .navbar-collapse form[role="search"].active {
    width: 40%;
}
        .navbar-collapse form[role="search"].active button,
        .navbar-collapse form[role="search"].active input {
            display: table-cell;
            opacity: 1;
        }
        .navbar-collapse form[role="search"].active input {
            width: 100%;
        }
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!--Customfontsforthistemplate--><linkrel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Muli" rel="stylesheet">

<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script><formclass="navbar-form" role="search" action="">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Procurar">
                        <span class="input-group-btn">
                            <button type="reset" class="btn btn-default">
                                <span class="glyphicon glyphicon-remove">
                                    <span class="sr-only">Close</span>
                                </span>
                            </button>

 <button type="submit" class="btn btn-default active">
                                <span class="fas fa-search procurar">
                                    <span class="sr-only">Search</span>
                                </span>
                            </button>
                        </span>
                    </div>
                </form>
                
  <div><p id='showSearchTerm'></p></div>

I put it like this:

$(document).on('click', 'button[type="submit"]', function(event) {

or you can switch to

$(document).on('click', '#id-element', function(event) {

and give an id pro button

Type:

<button type='button' id='id-element'> oi </button>
    
02.10.2018 / 16:22