2014-03-28 54 views
0

這裏是我的HTML如何在選擇標籤中放入AJAX響應?

<select class="expand" id="search_category" style="display: none;"> 
<option value=""></option> 
<option value="Sports">Sports</option> 
<option value="Arts & Music">Arts & Music</option> 
<option value="Academic & Tech">Academic & Tech</option> 
<option value="Odd Jobs">Odd Jobs</option> 
</select> 

當我選擇任何選項,有變化的jQuery AJAX調用。這裏是JSON響應

["Lacrosse","Squash","Basketball","Swimming","Golf","Tennis","Football","Other","Baseball"] 

我也試圖讓像這樣

<option value=""></option> 
<option value="Lacrosse">Lacrosse</option> 
<option value="Squash">Squash</option> 
<option value="Basketball">Basketball</option> 
<option value="Swimming">Swimming</option> 
<option value="Golf">Golf</option> 
<option value="Tennis">Tennis</option> 
<option value="Football">Football</option> 
<option value="Other">Other</option> 
<option value="Baseball">Baseball</option> 

HTML響應,但沒有把這兩種反應到下面的HTML選擇標籤。

<select id="showSubCats"></select> 

jQuery運行正常。我可以提醒這些值。如何將這些值放入選擇標籤中?

<select id="showSubCats"> 
<option value=""></option> 
<option value="Lacrosse">Lacrosse</option> 
<option value="Squash">Squash</option> 
<option value="Basketball">Basketball</option> 
<option value="Swimming">Swimming</option> 
<option value="Golf">Golf</option> 
<option value="Tennis">Tennis</option> 
<option value="Football">Football</option> 
<option value="Other">Other</option> 
<option value="Baseball">Baseball</option> 
</select> 

而jQuery的

jQuery(function(){ 
    jQuery('#search_subcategory').css('display','none'); 
    jQuery('#search_category_loader').css('display','none'); 
    jQuery('#search_category').on('change',function(){ 
     var category = jQuery(this).val(); 
     jQuery('#search_category_loader').css('display','block'); 
     jQuery.ajax({ 
      type: 'POST', 
      data: {'ajaxreturn':131, 'searchCat': category}, 
      url: '<?php bloginfo('url') ?>/', 
      //success:function(result){ 
      success: function(data){ 
       jQuery('#showSubCats').html(data); 
       jQuery('#search_subcategory').css('display','block'); 
       jQuery('#search_category_loader').css('display','none'); 
      }, 
      dataType: "html" 
     }); 
    }); 
}); 
+0

請武官JQuery的 – knightsb

+0

jQuery是連接我可以提醒這些值。 – Riz

+0

我編輯了我的問題,並且已經寫了我的問題 – Riz

回答

0

一個similar question可以幫助你,爲 方便找你,試試這個:

$("#showSubCats").empty().append(data); 
+0

其工作,謝謝 – Riz