简体   繁体   English

我正在从html select调用一个jquery函数,并访问jquery函数中的某些对象。 但是是不确定的

[英]I am calling a jquery function from html select and accessing some object in jquery function. but is comes undefined

here is my jquery function 这是我的jQuery函数

<script type="text/javascript">
    $('#branddrop').change(function getModels(){
        console.log('entered')
        brand = $(this).val();
        console.log(brand);
        console.log("http://localhost:8006/api/models/" + String(brand['name']));
        $.get("http://localhost:8006/api/models/", 
        function(data) {
            var models = $('#model');

            models.empty();

            $.each(data, function(i, value) {       
                models
                    .append($("<option></option>")
                    .attr("value",value.name)
                    .text(value.name)); 
            });

        });
    });
</script>

I am able to console the brand as 我能安慰brand

{"id":5,"name":"LeEco","created_at":null,"updated_at":null}

but when console the brand['name'] i get undefined. 但是当控制台brand['name']我不确定。

Looks like the brand variable holds the string value '{"id":5,"name":"LeEco","created_at":null,"updated_at":null}' 看起来品牌变量包含字符串值'{"id":5,"name":"LeEco","created_at":null,"updated_at":null}'

You might have to parse the string into an object as follows: 您可能必须按如下所示将字符串解析为一个对象:

var brandObj = JSON.parse(brand);
brandObj['name'] // should work

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

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