2017-04-03 69 views
0

只有在尚未完成時纔想填充選擇#current_location僅在選項標記不存在時填充選擇

$(document).ready(function(){ 
      $('#current_location').on('change', function(){ 
        /* 
        * Populate the select 
        * I want to populate the select only if it hasn't been done yet 
        */ 
        $.getJSON('fetch.php', {action: 'homepage' , key: 'current_location' }, function(data){ 
         var options = ''; 
         for (var x = 0; x < data.length; x++) { 
          options += '<option value="' + data[x] + '">' + data[x] + '</option>'; 
         } 
         $('#current_location').html(options); 
        }); 


       $.getJSON('fetch.php', {action: 'homepage' , key: $('#current_location').val() }, function(data){ 
        var options = ''; 
        for (var x = 0; x < data.length; x++) { 
         options += '<option value="' + data[x] + '">' + data[x] + '</option>'; 
        } 
        $('#destination').html(options); 
       }); 

      }); 
     }); 
+0

@SougataBose發帖作爲答案,以便我可以關閉這個 – user2650277

回答

0

檢查,如果沒有option的現在與相應增加option秒。

if($('#current_location').find('option').length < 1) { 
    .... // add options 
} 
0

檢查option長度。

var length = $('#YourSelectList') 
    .children('option') 
    .length; 
if (length > 0) { 

} else { 

}