简体   繁体   English

刷新OnKeyUp搜索过滤器的结果

[英]Refresh the result of OnKeyUp Search Filter

I have the onkeyup search function its working fine without any issue. 我有onkeyup搜索功能,其工作正常,没有任何问题。 I want that when I will write in a textbox and there is a part of result I want to keep updating without reloading full page. 我希望当我在文本框中写入并且有一部分结果时我想继续更新而不重新加载整页。

I have used the below code for refresh automatically after xxx seconds, it is refreshing the part of div using the below code of refresh but on key change, not automatically. 我已经使用下面的代码在xxx秒后自动刷新,它使用下面的刷新代码刷新div的部分,但是在键更改时,不是自动刷新。 I need it to be refreshing automatically after xxx seconds. 我需要它在xxx秒后自动刷新。

In the result there is close and open button if office time is still valid or closed. 如果办公时间仍然有效或关闭,结果会有关闭和打开按钮。 if i searched with OnKeyUp and it showed me the result with TRAVEL office is open and after 5 seconds its timing will finish for that i need to keep that refresh code to work on that time when the time will finish and will refresh it. 如果我用OnKeyUp搜索并且它向我显示TRAVEL办公室的结果是打开的,并且在5秒后它的计时将完成,我需要保持刷新代码在时间结束时工作,并将刷新它。

help is needed in this, please if somebody can adjust it. 如果有人可以调整,请在这需要帮助。

Code for refresh: 刷新代码:

$(document).ready(function() {
  $.ajaxSetup({ cache: false });
  setInterval(function() {
    $('#pen').load('sample.php');
  }, 1000); // the "3000" 
});

HTML HTML

<form class="well-home span6 form-horizontal">
<input type="text" id="book" onKeyUp="book_suggestion()">
</form>
<!-- Display Result of onkeyup Search -->
<div class="check" id="suggestion">
<!-- Refresh here -->
<div class="row check" id="pen">

</div>
</div>

JS of onkeyup onkeyup的JS

function book_suggestion() {
var book = document.getElementById("book").value;
var xhr;
 if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
var data = "textboxSearch=" + book;
     xhr.open("POST", "sample.php", true); 
     xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
     xhr.send(data);
     xhr.onreadystatechange = display_data;
    function display_data() {
     if (xhr.readyState == 4) {
      if (xhr.status == 200) {
      document.getElementById("suggestion").innerHTML = xhr.responseText;
      document.getElementById("suggestion").load = xhr.responseText;
      } else {
        alert('There was a problem with the request.');
      }
     }
    }
}

This is a typicall example, how to confuse yourself by mixing the jQuery with vanilla javascript and on..... event on an element. 这是一个典型的例子,如何通过混合jQuery和vanilla javascript以及on.....元素on.....事件来迷惑自己。 Once, you use jQuery, let use it's advantages. 有一次,你使用jQuery,让它使用它的优点。

First of all, organize your things, and use only jQuery, remove the onKeyUp from element. 首先,组织你的东西,只使用jQuery,从元素中删除onKeyUp。

//Setinterval
var timer = setInterval(function () {
    $('#pen').load('sample.php');
}, 1000);

$('#book').on('keyup', function() {

    $('#suggestion').load('sample.php', {action: 'onkeyup',  textboxSearch: $(this).val()}, function(response) {
        console.log('Response of sample: ' + response + ' if you need');
    });
});

But, your main problem is that you update $('#pen') in your timer, what is ok, but when you execute the keyup it is update the whole #suggestion so the $('#pen') will loss. 但是,你的主要问题是你在计时器中更新了$('#pen') ,没关系,但是当你执行keyup它会更新整个#suggestion所以$('#pen')会丢失。

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

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