简体   繁体   English

jQuery自动完成获取价值

[英]Jquery autocomplete get value

auto complete is working fine. 自动完成工作正常。 I put a hidden value for saving the value of auto complete by this method my code : 我用我的代码通过此方法放置了一个隐藏值来保存自动完成的值:

$("#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) ;
    }

    }); 

then I want to get this location Id for saving the value of location , so I tried the as: 然后我想获取此位置ID来保存location的值,因此我尝试了as:

save(){
var locationValue=$("#theHidden").val(); 
//other saving codes
}

but I got here locationValue is undefined . 但是我到这里locationValue是不确定的。

How I get the this hidden value in save function ? 我如何在保存功能中获得此隐藏值? autocomplete function is in document on ready and save function is in a js. 自动完成功能在文档中准备就绪,保存功能在js中。

this solved 这个解决了

select : function(event, ui) {

        setLocationValue(ui.item.key);
    }

in js 在js中

var locationValue;

function setLocationValue(value){
    locationValue=value;
}

In the response of the $ajax method you are mapping the data to a JSON object with only two properties, value and key. 在$ ajax方法的响应中,您将数据映射到仅具有两个属性(值和键)的JSON对象。

Then, in the select, you are using a property that doesn't exist anymore. 然后,在选择中,您使用的是不再存在的属性。

If you want those original values to still exist, you should map them as well. 如果希望这些原始值仍然存在,则也应映射它们。 Alternatively, you could save the id to the hidden field like this: 或者,您可以将id保存到隐藏字段中,如下所示:

$("#theHidden").val(ui.item.key);

Let me know if that doesn't work for you. 让我知道这是否不适合您。

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

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