简体   繁体   English

jQuery PHP实时搜索文本在搜索栏中消失

[英]Jquery php Live search text disappears in search bar

I am new to jquery and am trying to make a live search from my existing search page where you have to press enter. 我是jquery的新手,正在尝试从现有的搜索页面中进行实时搜索,您必须在其中按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. 我尝试将搜索值发布为get变量,并将其显示为搜索栏值,并在每次重新加载页面时将其自动聚焦到搜索栏中,这会将输入栏放在用户​​输入内容之前的表单开头。

I feel like I am approaching this the wrong way please help. 我觉得我正在以错误的方式处理此问题,请帮忙。 :) :)

You need to post your form via ajax. 您需要通过ajax发布表单。 Here's a simple fiddle to demonstrate this: 这是一个简单的小提琴来演示这一点:

http://jsfiddle.net/e76d09vw/ http://jsfiddle.net/e76d09vw/

HTML: 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: 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); 
        }           
    });    

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM