大家好,我想在 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>
它在 FF 和 Chrome 中运行良好,而不是在 Internet Explorer 中,因为我认为 html 存在问题。我尝试过:
$('#city').empty().append(data);
但是问题没有解决
如果我提醒数据,这是我的输出:
<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>