Json使用未定义填充下拉列表 [英] Json filling dropdown with undefined

查看:61
本文介绍了Json使用未定义填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Json用未定义填充了我的下拉列表.

Json fills my Drop-down with undefined.

<option value="undefined">undefined</option>
<option value="undefined">undefined</option>
<option value="undefined">undefined</option>
etc

我使用下面的代码填充我的下拉列表;

I use the follwing code to fill my Drop-Down;

$("select[name$='product_type']").change(function(){
    var url = "/all_json_models/" + $(this).val();
    var product_type = $(this).val();
    $.getJSON(url, function(products){
        var options = '<option value="Z">Select a type</option>';
        for (var i = 0; i < products.length; i++) {
            options += '<option value="' + products[i].pk + '">' + products[i].product_description + '</option>';
        }
        $("select#id_orderline_set-0-product").html(options);
        $("select#id_orderline_set-0-product option:first").attr('selected', 'selected');
        $("select#id_orderline_set-0-product").attr('disabled', false);
    });    
})

功能(产品)的内容;

The contents of function(products);

[{
    "fields": {
        "timestamp_created": "2014-09-30T20:20:06.912Z",
        "timestamp_updated": "2014-09-30T20:20:06.912Z",
        "product_price": "102",
        "product_type": 6,
        "product_description": "Roef 95X25X6"
    },
    "model": "product.product",
    "pk": 9
},
{
    "fields": {
        "timestamp_created": "2014-10-23T19:27:37.570Z",
        "timestamp_updated": "2014-10-23T19:27:37.571Z",
        "product_price": "34",
        "product_type": 6,
        "product_description": "dfsfgsdfg"
    },
    "model": "product.product",
    "pk": 20
}]

我在这里缺少什么,所以下拉菜单使用pk和产品说明?

What am I missing here so the Drop-Down uses the pk and product description?

当运行console.log(products[i]);时,我在控制台中得到以下结果;

when running console.log(products[i]); i get the following result in console;

[ ordercalculations.js:35
{ ordercalculations.js:35
" ordercalculations.js:35
f ordercalculations.js:35
i ordercalculations.js:35
e ordercalculations.js:35
l ordercalculations.js:35
d ordercalculations.js:35
s ordercalculations.js:35
" ordercalculations.js:35
etc

添加了我的views.py

added my views.py

def all_json_models(request, id=None):
    if id:
        current_product_type = ProductType.objects.get(pk=id)
    else:
        current_product_type = ProductType()
    products = Product.objects.all().filter(product_type=current_product_type)
    products_serial = serializers.serialize("json", products)
    return HttpResponse(
                json.dumps(products_serial), content_type='application/json'
     )

推荐答案

尝试一下

$("select[name$='product_type']").change(function(){
    var url = "/all_json_models/" + $(this).val();
    var product_type = $(this).val();
    $.getJSON(url, function(products){
        products = $.parseJSON( products );
        var options = '<option value="Z">Select a type</option>';
        for (var i = 0; i < products.length; i++) {
            options += '<option value="' + products[i].fields.pk + '">' + products[i].fields.product_description + '</option>';
        }
        $("select#id_orderline_set-0-product").html(options);
        $("select#id_orderline_set-0-product option:first").attr('selected', 'selected');
        $("select#id_orderline_set-0-product").attr('disabled', false);
    });    
});

这篇关于Json使用未定义填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆