jQuery的JSON对象if语句

问题描述 投票:2回答:1

我最近刚开始熟悉JSON,我试图找出我是否可以使用JSON对象内部的IF语句。我希望我正确地指这一点。

所以,在我的代码,我显示SELECT和它的选项值用JSON和我遇到的问题是,如果一个值为NULL,显示文本NONE。

这是我到目前为止有:

 function renderIPIEditView ($td)
 {
   var value = $td.text();
   $td.html(''); // clears the current contents of the cell
   var $select = $('<select class="form-control"></select>');
   $td.append($select);

   $.getJSON( 'api/inlands.php', function( data )
   {
     $.each(data, function(index, item)
     {  // here is where I think the IF statement should go
       $('<option>').
         attr('value', item.POINT_CODE).
         text(item.FULL_NAME+', ' +item.US_STATE).
         appendTo($select);
     });

     $select.val(value);
   });
 }

因此,与上面的代码,我可以显示必要的选项的值和文本。然而,在表内,有一个现在没有的POINT_CODE,并且会列出文本FULL_NAME和US_STATE为NULL,在屏幕上NULL一个记录。

我想我需要在这里我列在上面的代码中的注释IF语句的权利,但我不知道如果我甚至可以使用IF语句,更不用说如何在这种情况下使用它。

javascript jquery json if-statement
1个回答
4
投票

没有JSON特定它 - 它是一个基于对象的属性一个简单的if

$.each(data, function(index, item)
{ 
  if (item.POINT_CODE != 'NONE')
  {
    $('<option>').
      attr('value', item.POINT_CODE).
      text(item.FULL_NAME + ', ' + item.US_STATE).
      appendTo($select);
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.