简体   繁体   English

jQuery AJAX在页面加载时加载json内容,宁愿在搜索框中键入时加载它

[英]jQuery AJAX loads json contents at page load, would rather load it when typing in search box

I'm not sure why this is happening, but when my page loads, there's an XHR request immediately for the search results. 我不确定为什么会这样,但是当我的页面加载时,会立即为搜索结果发出XHR请求。 It's invisible to the user, but it's loading a fairly large chunk of json data. 它对用户是不可见的,但它正在加载相当大的json数据块。

Here's my code: 这是我的代码:

$.ajax({
  type: "POST",
  url: "http://localhost:8888/index.php/ajax/get_client",
  dataType: "json", data: "{}",
  success: function(data) {
    $('#search').autocomplete({
        source:data,
        minLength:2,
        delay:0,
        appendTo:'header',
        selectFirst:true,
        select:function(event, ui) {
            $("input#search").val(ui.item.value);
            $("#search").closest('form').submit();
        }
    });
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(errorThrown);
  }
});

How can I make it so the json data is only requested when the user types in the input#search box? 我怎样才能这样做只有当用户在输入#搜索框中键入时才会请求json数据?

It looks like you want to load a list autocomplete results and then initialize the autocomplete plugin only if the user starts typing. 看起来您想要加载列表自动完成结果,然后仅在用户开始输入时初始化自动完成插件。 To do this, bind a keyup function to the search box, if the results have not been loaded, then load the results and initialize the plugin. 为此,将键盘功能绑定到搜索框,如果尚未加载结果,则加载结果并初始化插件。

$(document).ready(function(){
    var searchInput = $("input#search");
    function loadData(onSuccess){
        $.ajax({
          type: "POST",
          url: "http://localhost:8888/index.php/ajax/get_client",
          dataType: "json", data: "{}",
          success: onSuccess,
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
          }
        });
    }
    function initializeAutocomplete(data){
         searchInput.addClass('loaded').autocomplete({
            source:data,
            minLength:2,
            delay:0,
            appendTo:'header',
            selectFirst:true,
            select:function(event, ui) {
                searchInput.val(ui.item.value).closest('form').submit();
            }
        });
    }
    searchInput.keyup(function(){
        if($(this).is(".loaded")) return;
        loadData(initializeAutocomplete);
    });
});

将ajax调用包装到button.click()事件中,或者如果您希望在用户键入时调用它,请将其放在textbox.keypress()事件中。

You need to bind a keyup event listener to your input box. 您需要将keyup事件侦听器绑定到输入框。 If you've inserted this code right inside your html page without a event listener, the code will execute right after your page loads. 如果你在没有事件监听器的情况下在html页面中插入了这段代码,代码将在你的页面加载后立即执行。

This should probably work: http://jsfiddle.net/XNbrX/ 这应该可行: http//jsfiddle.net/XNbrX/

I haven't tested it. 我没有测试过。

I don't know if I understand you, but I think that you want to run this code on every key press (really key up) to load results on every change of search box value. 我不知道我是否理解你,但我认为您希望在每次按键(真正按键)上运行此代码,以便在每次更改搜索框值时加载结果。 If I'm right, put your code into function which is triggered on 'onkeyup' event. 如果我是对的,请将您的代码放入'onkeyup'事件触发的函数中。

$('input#search-box').keyup(function() {
    $.ajax({
    type: "POST",
      url: "http://localhost:8888/index.php/ajax/get_client",
      dataType: "json", data: "{}",
      success: function(data) {
        $('#search').autocomplete({
            source:data,
            minLength:2,
            delay:0,
            appendTo:'header',
            selectFirst:true,
            select:function(event, ui) {
                $("input#search").val(ui.item.value);
                $("#search").closest('form').submit();
            }
        });
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(errorThrown);
      }
    });
});

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

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