Ajax如果在我使用GET时不起作用

时间:2019-02-12 09:09:27

标签: javascript ajax

这是我的Ajax函数和JSON数据。我想在2个输入字段中打印值。第一个输入字段已更新,但第二个字段未更新。

<select name="products" required class="form-control changeStatus"id="major_type_selected" onchange="get_inventory_check_for_order_ajax(this)">
    <option value="None">--Select a Type--</option>
    <option value="1">123</option>
    <option value="2">456</option>                                 
</select>
<input class="form-control sub_type_data"  value="0" type="number" name="wh_qty"/> 
<input class="form-control sub_type_datas" id="sub_type_datas" value="0"  type="number" name="SL_qty"/>

这是JSON数据:

  

“ [{{\” model \“:\” inventory.productlocation \“,\” pk \“:14,\” fields \“:   {\“位置\”:\“ IU \”,\“数量\”:0,\“产品\”:5}},{\“型号\”:   \“ inventory.productlocation \”,\“ pk \”:13,\“ fields \”:{\“ location \”:   \“ SL \”,\“数量\”:5,\“产品\”:5}},{\“型号\”:   \“ inventory.productlocation \”,\“ pk \”:15,\“ fields \”:{\“ location \”:   \“ WH \”,\“数量\”:6,\“产品\”:5}}]“

这是JavaScript代码

function get_inventory_check_for_order_ajax(ref) {

            var thisIs = $(ref);
            products = thisIs.val();

            console.log(products);


            $.ajax({
                'url': "{% url 'get_inventory_check_for_order' %}",
                'type': "GET",
                'data': {"products":products},
                'async': false,
                'success': function (data) {
                    if (data == "empty") {
                        $('#sub_type_data').html("");
                        $('#sub_type_data').append($('<option/>').attr("value", "None").text("--Select Wharehouse--").prop('selected', true).prop('disabled', true));
                    } else {
                        results = JSON.parse(data)
                        $.each(results, function(index, result){
                            console.log(result.fields.location);
                            if (result.fields.location == 'WH'){
                     thisIs.parent().next().find(".sub_type_data").val(result.fields.quantity);

                            }
                            else if (result.fields.location == 'SL'){
                                # Here the second input field's value should change
                                thisIs.parent().next().find(".sub_type_datas").val(result.fields.quantity);
                            }
                        });
                    }
                }
            });
        }

2 个答案:

答案 0 :(得分:1)

您所需的html位于下一个元素中。

所以:pd.merge_asof

答案 1 :(得分:0)

在没有空关键字的情况下使用

使用:

if (data){}  //this will run if "data" has some value otherwise else statement is encountered