2017-04-03 10 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 sがありませんし、それに応じてoption Sを追加するかどうかを確認します。

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

チェックoption長さ。

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

} else { 

} 
関連する問題