M 使用 ajax 来填充选择标签下拉菜单。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标记为“未找到此选择的数据”` $(文档).ready(函数(){ $("#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>'); } } } }); });});`在此处输入图像描述 现在写入 它在没有数据时显示空白
1 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
在循环响应之前,您必须检查它的长度。没有检查这段代码,但它应该工作
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 关注
- 82 浏览
添加回答
举报
0/150
提交
取消