M 使用 ajax 來(lái)填充選擇標(biāo)簽下拉菜單。Selecting an option from one select tag will fetch data to the next select tag from database through ajax.. M able to get the result but when an option is selected for which no data is available in database i want to show some message in the select標(biāo)記為“未找到此選擇的數(shù)據(jù)”` $(文檔).ready(函數(shù)(){ $("#sel_block").change(function(){ var blockid = $(this).val(); $.ajax({ url: 'getZone.php', type: 'post', data: {block:blockid}, dataType: 'json', success:function(response){ var len = response.length; $("#sel_zone").empty(); for( var i = 0; i<len; i++){ var id = response[i]['id']; var name = response[i]['name']; var temp="No Data Found"; if(response.length) { $("#sel_zone").append("<option value='"+id+"'>"+name+"</option>"); } else { $("#sel_zone").append('<option value="">' + emptyMessage + '</option>'); } } } }); });});`在此處輸入圖像描述 現(xiàn)在寫(xiě)入 它在沒(méi)有數(shù)據(jù)時(shí)顯示空白
1 回答

有只小跳蛙
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
在循環(huán)響應(yīng)之前,您必須檢查它的長(zhǎng)度。沒(méi)有檢查這段代碼,但它應(yīng)該工作
var len = response.length;
$("#sel_zone").empty();
if(len == 0){
$("#sel_zone").append('<option value="">No Data Found</option>');
} else{
for( var i = 0; i<len; i++){
var id = response[i]['id'];
var name = response[i]['name'];
$("#sel_zone").append("<option value='"+id+"'>"+name+"</option>");
}
}
- 1 回答
- 0 關(guān)注
- 113 瀏覽
添加回答
舉報(bào)
0/150
提交
取消