大家好,我有一个网站,如果我改变一个选择,就会启动一个 ajax 调用,它会返回一个选择。想要复制它的选项并插入到我现有的选择中。这是我的代码:
$(document).ready(function(){
$("#nation").change(function() {
var nation_id = $(this).val();
change_city_by_nation(nation_id);
});
function change_city_by_nation(nation_id){
var site_url_city ="<?php echo(site_url('/backend/city/get_city_by_nation_id')); ?>";
$.ajax({
url: site_url_city,
async: false,
type: "POST",
data: "nation_id="+nation_id,
dataType: "html",
success: function(data) {
$('#city').html(data);
$('#city').prepend("<option value='0' selected='selected'>All city</option>");
}
});
}
});
<select name="nation" id="nation" style="width:180px;" >
<option value="2">Nation2</option>
<option value="3">Nation3</option>
</select>
<select name="city" id="city" style="width:180px;" >
<option value="0">All city</option>
</select>
数据警报是:
<select name="citylist">
<option value="39">Adelaide</option>
<option value="43">Alice Springs</option>
<option value="44">Brisbane</option>
<option value="45">Cairns</option>
<option value="36">Canberra</option>
<option value="31">Darwin</option>
<option value="37">Gold Coast</option>
<option value="32">Margaret River - Regione dei Vini</option>
<option value="40">Melbourne</option>
<option value="41">Palm Cove</option>
<option value="35">Perth</option>
<option value="42">Port Douglas</option>
<option value="33">Sydney</option>
<option value="34">Tasmania</option>
<option value="38">Townsville</option>
</select>
如何将从数据返回我的选项复制到成功函数中选择城市?因为我以我的方式将一个选择放入另一个选择中。我无法更改 ajax 的结果