dqys98341 2014-12-30 23:02
浏览 33
已采纳

Jquery php实时搜索文本在搜索栏中消失

I am new to jquery and am trying to make a live search from my existing search page where you have to press enter.

This is the code I have made so far.

$( document ).ready(function() {


   $('#searchhh').on("input", function() {
   var searchquery = this.value;
   $( "#searchform" ).submit();


   });
});

This gets the input of the form and searches it every key stoke. The problem is the text that the user types resets every time the form submits. I tried to post the search value as a get variable and display it as the search bars value and auto focus into the the search bar every time the page reloads, this puts the typing bar at the beginning of the form before what the user has typed.

I feel like I am approaching this the wrong way please help. :)

  • 写回答

1条回答 默认 最新

  • douyao4632 2014-12-31 02:28
    关注

    You need to post your form via ajax. Here's a simple fiddle to demonstrate this:

    http://jsfiddle.net/e76d09vw/

    HTML:

    <form id="searchform" method="post" action="some_url_here">
        <input type="text" name="search" placeholder="Search..." />
    </form>
    
    Results:<br>
    <div id="results"></div>
    

    JS:

    $('#searchform').on("keyup", "input", function() {
    
        var form_data = $("#searchform").serialize();
        var form_url = $("#searchform").attr("action");
        var form_method = $("#searchform").attr("method").toUpperCase();
    
        $.ajax({
            url: form_url, 
            type: form_method,      
            data: form_data,     
            cache: false,
            success: function(returnOutput){ 
                // Your search results are outputted here
                $("#results").html(returnOutput); 
            }           
        });    
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面