我是Django和Ajax的新手。我有一个从数据库中填充的下拉列表。我希望根据下拉列表中的选择填充另一个字段。我正在获取返回值,但它没有填充表单字段。Console.log显示返回的值。它只是从不显示在表单字段上,我确信字段名称是正确的。在浏览器中我得到600。如何将600输入#id_Ductable字段?如有任何帮助,我们将不胜感激。
`
<form method="POST" id="BaseQuoteForm" data-agent-url="{% url 'ajax-load-agent' %}">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-success btn-block" type="submit" value="Add">
</form>
<script
src="https://code.jquery.com/jquery-3.6.1.js"
integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI="
crossorigin="anonymous"></script>
<script>
$("#id_Agency").change(function () {
var url = $("#BaseQuoteForm").attr("data-agent-url"); // get the url of the `load_cities` view
var Agency = $(this).val(); // get the selected country ID from the HTML input
$.ajax({ // initialize an AJAX request
url: url, // set the url of the request (= localhost:8000/hr/ajax/load-cities/)
data: {
'Agency_id': Agency // add the country id to the GET parameters
},
success: function (data) { // `data` is the return of the `load_cities` view function
console.log(data);
$("#id_Agent").html(data); // replace the contents of the city input with the data that came from the server
}
});
});
</script>
<script>
$("#id_Primary_Plan").change(function () {
var url = $("#BaseQuoteForm").attr("data-agent-url"); // get the url of the `load_cities` view
var PlanData = $(this).val(); // get the selected country ID from the HTML input
$.ajax({ // initialize an AJAX request
url: url, // set the url of the request (= localhost:8000/hr/ajax/load-cities/)
data: {
'PlanData_id': PlanData // add the country id to the GET parameters
},
success: function (data) { // `data` is the return of the `load_cities` view function
console.log(data);
$("#id_Deductable").html(data); // replace the contents of the city input with the data that came from the server
}
});
});
</script>
`