使用if / else语句获取空记录时显示警报

时间:2014-12-09 11:03:35

标签: javascript jquery

如果record具有null值,则它必须显示警告消息,代码不起作用。请考虑以下代码:

function DiseasesByName() {
    var dsName = $('#dsName').val();
    $.getJSON('http://54.148.253.123/edoctor/HHS_Service/HealthService.svc/DiseasesByName', {
        diseaseName: dsName
    }, function(data) {
        var tasks = $.parseJSON(data.d);
        $("#DiseasesList").empty();
        $.each(tasks, function(key, value) {
            if (data.d != 'null') {
                $('<div data-role="collapsible" data-content-theme="a" data-collapsed="true"><h3>' + value.diseaseName +
                    '</h3><ul data-role="listview" id="diseaseListView" data-inset="true" data-theme="a"><li><strong>Detail:</strong><span>' + value.description + '</span></li></ul></div>').appendTo('#DiseasesList');
                // refreshing collapsible created dynamically "its necessary to refresh for a jQuery look and feel"
                $('div[data-role=collapsible]').collapsible({
                    theme: 'b',
                    refresh: true
                });
            } else {
                alert('No record Found');
            }
            $("#clear").click(function() {
                $("#DiseasesList").empty();
                $("#dsName").val('');
            });
        });

    });
}

2 个答案:

答案 0 :(得分:3)

您需要与null本身进行比较,而不是字符串"null"

if(data.d!=null)

答案 1 :(得分:0)

有多个问题

  • null不是字符串值,因此您的比较应为data.d != null
  • 应该在每个循环中进行比较,因为如果不存在data.d,则没有任何内容可以循环
  • 应该在方法之外添加清除点击处理程序,因为该元素不是动态创建的

所以

&#13;
&#13;
function DiseasesByName() {
  var dsName = $('#dsName').val();
  $.getJSON('http://54.148.253.123/edoctor/HHS_Service/HealthService.svc/DiseasesByName', {
    diseaseName: dsName
  }, function(data) {
    //do it before the loop
    var array = data.d ? JSON.parse(data.d) : [];
    if (array && array.length) {
      $("#DiseasesList").empty();
      $.each(JSON.parse(data.d), function(key, value) {
        $('<div data-role="collapsible" data-content-theme="a" data-collapsed="true"><h3>' + value.diseaseName +
          '</h3><ul data-role="listview" id="diseaseListView" data-inset="true" data-theme="a"><li><strong>Detail:</strong><span>' + value.description + '</span></li></ul></div>').appendTo('#DiseasesList');
        // refreshing collapsible created dynamically "its necessary to refresh for a jQuery look and feel"
        $('div[data-role=collapsible]').collapsible({
          theme: 'b',
          refresh: true
        });
      });
    } else {
      alert('No record Found');
    }
  });
}

jQuery(function() {
  //it should be out of the `DiseasesByName` method
  $("#clear").click(function() {
    $("#DiseasesList").empty();
    $("#dsName").val('');
  });

  $('#get').click(DiseasesByName)
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="dsName" />
<input id="get" type="button" value="get" />
<input id="clear" type="button" value="clear" />
<div id="DiseasesList"></div>
&#13;
&#13;
&#13;