2

我从 XML 获取数据。我需要检查数据,如果数据为空,我必须隐藏它。

我该如何检查?

  <script>
   downloadUrl("gxml.php", function(data) {
       var xml = data.responseXML;
       var markers = xml.documentElement.getElementsByTagName("marker");
       for (var i = 0; i < markers.length; i++) {
         var oid = markers[i].getAttribute("objectid");
         var status = markers[i].getAttribute("status");
         var title = markers[i].getAttribute("title");
         var volume = markers[i].getAttribute("volume");
         var kwh = markers[i].getAttribute("kwh");
         var puser = markers[i].getAttribute("puser");
         var ucomp = markers[i].getAttribute("ucomplate");
         var udate = markers[i].getAttribute("udate");
         var suser = markers[i].getAttribute("suser");
         var scomp = markers[i].getAttribute("scomplate");
         var sdate = markers[i].getAttribute("sdate");
            var type = markers[i].getAttribute("type");
         var point = new google.maps.LatLng(
             parseFloat(markers[i].getAttribute("lat")),
             parseFloat(markers[i].getAttribute("lng")));
         var html = "ObjectID:"  + oid + "  <br/> Title:"  + title +"  <br/>Status:" + status + "  <br/>Volume:" + volume + "  <br/> KWh:" + kwh + "  <br/>User:" + puser + "  <br/> User Date:" + udate + "  <br/> User Complate :" + ucomp + "  <br/>Super user:" + suser + "  <br/> S .User Date:" + sdate + "  <br/> S. User Complate :" + scomp + "<br/> <a href=\"edit.php\">Add/Edit</a>";

</script>

// this data i get from xml. i`m need to chek this null or not null
4

2 回答 2

3

您可以简单地if (!myVariable)检查任何给定的变量。如果返回 true(也就是说,如果代码进入if正文,则数据为空。我会做的不是这样做:

var oid = markers[i].getAttribute("objectid");
// ...
..."ObjectID:"  + oid + "

在变量中包含您的标题,如下所示:

var oid = markers[i].getAttribute("objectid");
if (oid) {
    oid = "ObjectID: " + oid;
}

这样你就可以oid直接添加到你的字符串中。如果该值为 null,则添加它将无效,因为该变量将为空。如果有一个值,则将附加标题,并且执行 a+ oid将同时附加标题和值。

于 2012-12-11T07:23:21.593 回答
0
if ( !foo ) {
    // foo is not set, it is null.
}
于 2012-12-11T07:24:02.417 回答