gpt4 book ai didi

jquery - 这些回调让我发疯

转载 作者:行者123 更新时间:2023-12-01 08:18:46 27 4
gpt4 key购买 nike

$("#to").autocomplete({

//define callback to format results
source: function(req, add) {

//pass request to server
$.getJSON("friends.php?callback=?", req, function(data) {

//create array for response objects
var suggestions = [];

//process response
$.each(data, function(i, val) {
suggestions.push(val.name);
});

//pass array to callback
add(suggestions);
});
}
});

Source

我试图熟悉自动完成功能(jquery-ui),然后我遇到了

function (req, add).

它如何知道req是输入的数据?

回调add在哪里定义?

有人可以给我指出一个有用的教程,以便我了解如何使用这些回调吗?

最佳答案

它来自自动完成插件。请参阅:http://jqueryui.com/demos/autocomplete

The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

  • A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo".
  • A response callback, which expects a single argument to contain the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data (String-Array or Object-Array with label/value/both properties). 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. This ensures that the widget always has the correct state.

更新:

您提供的回调函数 (source: function(req, add) {...}) 由插件调用,并且插件会传递这些参数的实参。不过,您可以自由选择参数的名称。

您可以找到source code on Github 。传递给回调的函数在 line 134 中定义。 :

this.response = function() {
return self._response.apply( self, arguments );
};

回调函数在 line 311 中被调用(this.source 是您的回调):

_search: function( value ) {
this.pending++;
this.element.addClass( "ui-autocomplete-loading" );

this.source( { term: value }, this.response );
},

关于jquery - 这些回调让我发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8725965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com