【发布时间】:2020-10-01 15:41:28
【问题描述】:
我有两个内置的 python 下拉菜单,我正在尝试使用 Ajax 调用将它们链接在一起。
可以在html中查看相关代码:
<script>
$("#id_city").change(function () { // event on the city dropdown change.
var cityId = $(this).val();
$.ajax({ // initialize an AJAX request
url: '{% url "city_autocomplete" %}', // set the url of the request (= localhost:5000/app/ajax/city-autocomplete/)
data: {
'cityId': cityId // add the city id to the GET parameters
},
// dataType: 'json',
success: function (data) {
$('#preview').html(data); // replace the html response with the <div> content
}
});
event.preventDefault();
});
</script>
<script>
$("#id_tag").change(function () {
var tagId = $(this).val();
$.ajax({
url: '{% url "city_autocomplete" %}',
data: {
'tag': tagId
},
success: function (data) {
$('#preview').html(data);
}
});
event.preventDefault();
});
</script>
一旦我选择了城市,cityId 会取值,但tagId 不会返回任何值,反之亦然。我想知道jQuery中的解决方案是什么,同时监听两个dorpdown的变化,或者说如何将两个on.change()合并在一起?
【问题讨论】: