如何将选定的值作为额外参数传递给ajax调用 [英] How to pass selected value as extra parameter to ajax call
问题描述
我的编码部分
$("#demo-input").tokenInput("data/autosuggest-search-city.php",
{
searchDelay: 2000,
minChars: 3,
tokenLimit: 10
});
我想将选定的值作为附加参数发送到"data/autosuggest-search-city.php".
I want to send the selected values as extra parameter to "data/autosuggest-search-city.php".
例如, 最初,我从列表中搜索并选择一个值 然后再次搜索,这次我想将选择的第一个值发送到服务器.
For example, Initially I search and select one value from list then again searching, this time I want to send the 1st selected value to server.
该怎么做?
推荐答案
TokenInput 插件本身不支持. 但是,只要添加或从选择中删除令牌,您都可以采用一种简单的变通办法来更新"AJAX网址" .
TokenInput plugin doesn't support that natively.
You can however make a simple workaround to update "AJAX url" whenever a token is added or removed from selection.
使用onAdd
和onDelete
回调触发"AJAX网址"更改;
使用selector.tokenInput("get")
方法;
获取选定的令牌
通过更新元素的.data("settings").url
来设置新的"AJAX网址" ;
Use onAdd
and onDelete
callbacks to trigger "AJAX url" changes;
Get selected tokens using selector.tokenInput("get")
method;
Set the new "AJAX url" by updating .data("settings").url
of the element;
// cache the original url:
var token_url = "data/autosuggest-search-city.php";
$("#demo-input").tokenInput(token_url, {
searchDelay : 2000,
minChars : 3,
tokenLimit : 10,
onAdd : function(){
urlChange.call(this);
},
onDelete : function(){
urlChange.call(this);
}
});
function urlChange(){
var tokens = '', token_list = $(this).tokenInput("get");
// proceed if any token/s are selected:
if(token_list.length){
// loop through the selected tokens (if more than one is selected)
$.each(token_list, function(i, token){
// use token "id" (or "name" if you wish) and separate them with comma:
tokens += token.id + ',';
});
// update url:
$(this).data("settings").url = token_url + '?selected_tokens='+tokens.replace(/,+$/,'');
}else{
// leave original url if no tokens are selected:
$(this).data("settings").url = token_url;
}
};
这篇关于如何将选定的值作为额外参数传递给ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!