[英]Jquery autocomplete get value
自動完成工作正常。 我用我的代碼通過此方法放置了一個隱藏值來保存自動完成的值:
$("#cityOp").autocomplete({
source : function(request, response) {
var city_value = jQuery("#cityOp").val();
$.ajax({
url: "city.html",
dataType: "json",
data : {
filter : city_value
},
success : function(data) {
response(jQuery.map(data.cities,function(item) {
return {
value : item.locationName,
key : item.locationId
};
}));
},
select : function(event, ui) {
$("#theHidden").val(ui.item.key) ;
}
});
然后我想獲取此位置ID來保存location的值,因此我嘗試了as:
save(){
var locationValue=$("#theHidden").val();
//other saving codes
}
但是我到這里locationValue是不確定的。
我如何在保存功能中獲得此隱藏值? 自動完成功能在文檔中准備就緒,保存功能在js中。
這個解決了
select : function(event, ui) {
setLocationValue(ui.item.key);
}
在js中
var locationValue;
function setLocationValue(value){
locationValue=value;
}
在$ ajax方法的響應中,您將數據映射到僅具有兩個屬性(值和鍵)的JSON對象。
然后,在選擇中,您使用的是不再存在的屬性。
如果希望這些原始值仍然存在,則也應映射它們。 或者,您可以將id保存到隱藏字段中,如下所示:
$("#theHidden").val(ui.item.key);
讓我知道這是否不適合您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.