Вот моя 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 \ ": \" инвентарь.productlocation \ ", \" pk \ ": 14, \" fields \ ": {\" location \ ": \" IU \ ", \" number \ ": 0, \" product \ ": 5}}, {\ "model \": \ "inventory.productlocation \", \ "pk \": 13, \ "fields \": {\ "location \": \ "SL \", \ "number \": 5, \"product \": 5}}, {\ "model \": \ "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);
}
});
}
}
});
}