[英]jQuery UI Autocomplete: How can I pass additional data with my Ajax call?
I have two jQuery UI Autocomplete widgets set up. 我有两个jQuery UI自动完成小部件设置。 With the first autocomplete, the user will enter a client's name - the autocomplete will narrow it down until the correct client has been chosen.
使用第一个自动完成功能,用户将输入客户端的名称 - 自动完成功能将缩小范围,直到选择了正确的客户端。 I then have a callback that takes the ID of the client returned and puts it into a hidden input field.
然后我有一个回调,它获取返回的客户端的ID并将其放入隐藏的输入字段。
Next, is a second autocomplete field. 接下来,是第二个自动完成字段。 When searched in, this needs to send two variables to the server - the user's search string (
term
), and the User ID of the client that was searched for previously. 搜索时,需要向服务器发送两个变量 - 用户的搜索字符串(
term
),以及之前搜索过的客户端的用户ID。
I have no problems dealing with the server side of things, but where I'm struggling is how to pass 2 variables to the Ajax call, rather than just term
. 我在处理服务器端方面没有问题,但是我正在努力的是如何将2个变量传递给Ajax调用,而不仅仅是
term
。 In my PHP backend, I need to query against the User ID as well, to only return properties belonging to that user. 在我的PHP后端,我还需要查询用户ID,只返回属于该用户的属性。
How can I do this? 我怎样才能做到这一点?
Thanks! 谢谢!
Edit: Thanks to @JohnP, this was what I ended up - seems to work fine for me. 编辑:感谢@JohnP,这就是我最终的结果 - 似乎对我来说很好。 Posting this here for reference for anyone in future who drops by:
在此发布此信息,供以后任何人使用:
source: function (request, response) {
var request_data = {
term: request.term,
client_id: $('input#client_id_string').val()
};
var url = 'http://mysite.com/search/ajax_search';
$.getJSON(url, request_data, function (data, status, xhr) {
response(data);
});
},
You can override the source
method if it's a static method. 如果它是静态方法,您可以覆盖
source
方法。
//code
source: function (request, response) {
var term = request.term;
//caching if yo uwant
var myCustomVar = 42;
$.getJSON(url + term + '/' + myCustomVar , request, function (data, status, xhr) {
response(data);
});
},
//code
You can either make it part of the URL or just pass it along with the request, if you want. 如果需要,您可以将其作为URL的一部分,或者只是将其与请求一起传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.