简体   繁体   English

jquery-ui自动完成的错误操作

[英]Incorrect operation of the jquery-ui autocomplete

Immediately after reloading the page when you enter a single character in the input, nothing happens. 在重新加载页面后,当您在输入中输入单个字符时,立即没有任何反应。 when you enter the following characters begins to complement. 当您输入以下字符时,将开始补充。

Get data 获取数据

`function getData(data, callback) {
            $.ajax({
                url: "myUrl" + encodeURIComponent(data),
                method: "GET",
                dataType: "JSON",
                success: callback
            })
        }`

Callback function 回调功能

 `function autocompleteInput () {
       var dataInput = $("#myInput").val();
       function success(data) {
         var dataArr = [];
         for (var i = 0; i < data.data.length; i++) {
           dataArr.push(data.data[i].name);
               }
                    $("#myInput").autocomplete({
                        source: brokersNameArr,
                        delay: 500,
                        minLength: 1
                    })
                  getData(dataInput, success);
       }`

Use in html 在html中使用

$("#myInput").keyup($.throttle(200, autocompleteInput));

Would suggest the following: 建议如下:

var dataArr = [];
$("#myInput").autocomplete({
  source: function(req, resp){
    $.getJSON("myurl?" + req.term, function(results){
      $.each(results.data, function(k, r){
        dataArr.push(r.name);
      });
      resp(results);
    });
  },
  delay: 500,
  minLength: 1
});

You might also want to review: http://jqueryui.com/autocomplete/#multiple-remote 您可能还需要查看: http : //jqueryui.com/autocomplete/#multiple-remote

Using a Function for source will give you the ability to manage how the data is sent and received. source使用功能将使您能够管理数据的发送和接收方式。

Function : The third variation, a callback, provides the most flexibility and can be used to connect any data source to Autocomplete, including JSONP. 功能 :第三个变体是回调,它提供了最大的灵活性,可用于将任何数据源(包括JSONP)连接到自动完成功能。 The callback gets two arguments: 回调有两个参数:

  • A request object, with a single term property, which refers to the value currently in the text input. 具有单个term属性的请求对象,它引用文本输入中当前的值。 For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo" . 例如,如果用户在城市字段中输入"new yo" ,则“自动完成”项将等于"new yo"

  • A response callback, which expects a single argument: the data to suggest to the user. response回调,它需要一个参数:向用户建议的数据。 This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. 该数据应根据提供的术语进行过滤,并且可以采用上述针对简单本地数据描述的任何格式。 It's important when providing a custom source callback to handle errors during the request. 提供自定义源回调以处理请求期间的错误时,这一点很重要。 You must always call the response callback even if you encounter an error. 即使遇到错误,也必须始终调用response回调。 This ensures that the widget always has the correct state. 这样可以确保小部件始终具有正确的状态。

Hope this helps. 希望这可以帮助。

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

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