Search in jquery on autocomplete login

I have jQuery that works great. It loads all the autocomplete values, and when you click on them, they load searchdata.php, which performs the entire search. It works.

What I canโ€™t understand is that if the user decides that he doesnโ€™t want to select the autocomplete value and press the enter key instead, how can I get him to execute searchdata.php on the value that is in the search input when he gets into it WITHOUT reloading the page? I tried to bind and live and cannot make it work.

<script type="text/javascript">
$(document).ready(function() {

var availableTags = <?php echo json_encode($findrow);?>;

  $("#search_input").watermark("Begin Typing to Search");
  $('#searchresultdata').append(sendSelected);

    $("#search_input").autocomplete({
        source: availableTags,
        select: function(event, ui) {
          sendSelected(ui.item.value);
          }
        });

function sendSelected(_val){
    var search_input = _val;
    if (search_input =='') search_input ='*';
    var dataString = 'keyword='+search_input;

    if (search_input.length > 2 || search_input=='*') {
       $.ajax({
            type: "GET",
            url: "core/functions/searchdata.php",
            data: dataString,
            success: function(server_response) {
                $('#searchresultdata').empty();
                $('#searchresultdata').append(server_response);
                $('span#category_title').html(search_input);
            }
        });
    }
}
});
</script>
+3
source share
3 answers
$("#search_input").keypress(function(e) {
  if(e.keyCode == 13)
     {
         e.preventDefault();
         sendSelected(this.value);
         $(this).autocomplete('close');
     }
});
+7
source
$("#search_input").on('keyup', function(event) {
  if(event.which == 13)  // event.which detect the code for keypress in jQuery
     {
         event.preventDefault();
         sendSelected(this.value);
     }
});
+3
source

event.preventDefault() , , , ,

http://api.jquery.com/event.preventDefault/

+2

All Articles