Stop reloading page with 'Enter Key'

前端 未结 9 895
臣服心动
臣服心动 2020-12-25 10:11

I have a search box at the top of page that makes an ajax call when a user hits the adjacent button. I am trying to update the input tag so that when a user hit the \'en

相关标签:
9条回答
  • 2020-12-25 10:56
     $('#seachForm').submit(function(e){
          e.preventDefault();
          //do something
     });
    
    0 讨论(0)
  • 2020-12-25 10:57

    Add onSubmit="return false;" on your form tag

    <form onSubmit="return false;">
    /* form elements here */
    </form>`
    
    0 讨论(0)
  • 2020-12-25 10:58

    You are missing # in the selector. Try this

    <input type='text' id='searchText' />
    

    JS

    $("#searchText").bind('keyup', function(event){ 
      if(event.keyCode == 13){ 
        event.preventDefault();
        //$("#buttonSrch").click(); 
        search(this.value);
      }
    });
    
    0 讨论(0)
  • 2020-12-25 10:58

    I know its a little late but I ran into the same problem as you. It worked for me using "keypress" instead of bind.

    $('#searchText').keypress(function (e) {                                       
           if (e.which == 13) {
                e.preventDefault();
                //do something   
           }
    });
    
    0 讨论(0)
  • 2020-12-25 11:00

    Just use the "action" attribute in <form> tag. Like this

    <form action="#">   // your content     </form>
    
    0 讨论(0)
  • 2020-12-25 11:05

    You could set the form action attribute to javascript:void(0); that way the form doesn't post/get so the page wont refresh.

    $(document).ready(function () {
      $('#form1').attr('action', 'javascript:void(0);');
    });
    
    0 讨论(0)
提交回复
热议问题