2015-02-05 92 views
0

檢索下拉我有這個表:jQuery的 - 無法與阿賈克斯

<table id="fla_inf" width="100%"> 
<tbody> 
<tr> 
<th class="tab_header" colspan="6">Flavors and Additives</th> 
</tr> 
<tr> 
<th class="tab_header_nam">Flavor Brand</th> 
<th class="tab_header_nam">Flavor Name</th> 
<th class="tab_header_nam">Dropper type</th> 
<th class="tab_header_nam">Quantity Unit</th> 
<th class="tab_header_nam">Quantity</th> 
<th class="tab_header_nam">Add/Remove row</th> 
</tr> 
<tr class="flavors"> 
<td>[brand_list]</td> 
<td><select id="arome0" class="arome"></td> 
<td><select id="dropper0" class="dropper"> 
<option selected="selected" value="type1">type 1</option> 
<option value="type2">type 2-3</option> 
</select></td> 
<td><select id="qtyunit0" class="qtyunit"> 
<option value="ml">ml</option> 
<option value="drops">drops</option> 
<option selected="selected" value="perc">%</option> 
</select></td> 
<td><input id="quantity0" class="quantity" type="number" /></td> 
<td><input class="addline" src="http://spitilab.com/wp-content/uploads/2015/01/add.png" type="image" /><input class="remline" src="http://spitilab.com/wp-content/uploads/2015/01/delete.png" type="image" /></td> 
</tr> 
</tbody> 
</table> 

我需要更新ID下拉列表「arome0」時,選擇與簡碼[brand_list產生的第一個值]。

我需要使用Ajax才能獲得品牌的子項並填充「arome0」下拉列表。

我創建了這個jquery代碼,當我的第一個下拉列表的值發生變化時調用。

//On selected brand, update flavors list 
$(document).on('change', "select[id^='marque']", function() { 

     var $brandid = $(this).val(); 
     var $brand_dd_id = $(this).attr('id'); 
     var $flav_dd_id = $brand_dd_id.substr($brand_dd_id.length-1); 
     $("#arome"+$flav_dd_id).empty(); 

     //Make AJAX request, using the selected value as the GET 
     $.ajax({ 
       type: 'GET', 
       data: '{"parent_id":"' + $brandid + '"","id":"'+ $flav_dd_id +'",action":"brand_children"}', 
       success: function(output) { 
        $("#arome"+$flav_dd_id).html(output); 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        alert(xhr.status + " "+ thrownError); 
     }}); 

}); 
在我的functions.php

我添加了這個:

add_action('wp_ajax_brand_children', 'GetBrandChildren'); 
add_action('wp_ajax_nopriv_brand_children', 'GetBrandChildren'); 

function GetBrandChildren($parent_id,$id) { 
    $children = wp_dropdown_pages(array('id'=>'arome$id','post_type'=>'aromes-type','child_of'=>$parent_id,'echo'=>0)); 
    return $children; 
} 

,但它不工作,我的事情有一個與數據的問題返回。 有什麼想法?

感謝

UPDATE:

它現在的工作,最終的問題是下拉不與父帖子的ID正確篩選。也許我沒有正確使用wp_dropdown_page。

更新的functions.php:

add_action('wp_ajax_brand_children', 'GetBrandChildren'); 
add_action('wp_ajax_nopriv_brand_children', 'GetBrandChildren'); 

function GetBrandChildren() { 
    $parent_id = $_POST['parent_id']; 
    $id = $_POST['id']; 
    echo wp_dropdown_pages(array("id"=>"arome$id",'post_type'=>'aromes-type','child_of'=>$parent_id,'echo'=>0)); 
    //ob_clean(); 
    //echo "working"; 
    wp_die(); 
} 

// need these two lines to be ale to locate the admin-ajax.php inside jquery 
wp_enqueue_script('my-ajax-request', get_template_directory_uri() . '/js/ajax.js', array('jquery')); 
wp_localize_script('my-ajax-request', 'MyAjax', array('ajaxurl' => admin_url('admin-ajax.php'))); 

更新的JQuery:

//On selected brand, update flavors list 
$(document).on('change', "select[id^='marque']", function() { 

     var $brandid = $(this).val(); 
     var $brand_dd_id = $(this).attr('id'); 
     var $flav_dd_id = $brand_dd_id.substr($brand_dd_id.length-1); 
     $("#arome"+$flav_dd_id).empty(); 

     //Make AJAX request, using the selected value as the GET 
     //var ajax_url = admin_url('admin-ajax.php'); 
     $.ajax({ 
       url: MyAjax.ajaxurl, 
       data: { 
         'parent_id': $brandid, 
         'id': $flav_dd_id, 
         'action': 'brand_children' 
         }, 
       success: function(output) { 
        console.log(output); 
        $("#arome"+$flav_dd_id).html(output); 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        alert(xhr.status + " "+ thrownError); 
     }}); 

}); 

最後一個問題是wp_dropdown_pages不與父ID過濾,也許我沒有正確地使用它。

+0

您可以在瀏覽器中查看開發者控制檯,看到很多情況。具體來說,你可以看到網絡調用和響應,看看頁面發送到服務器和服務器發回的數據/信息 – 2015-02-05 23:41:21

+0

(array('id'=>'arome $ id'是錯的,你不是正確地轉義字符串try(array(「id」=>「arome $ id」 – 2015-02-05 23:51:47

+0

我對'id'進行了修改,但它仍然無法正常工作,當我查看控制檯時,我看到了對admin-ajax的調用。 PHP的200返回碼,還有什麼我可以看看? – 2015-02-06 00:38:02

回答

-1

你似乎沒有任何元素與'標記'的id,所以,你的ajax調用將永遠不會觸發。另外,這是做什麼的? admin_url('admin-ajax.php');它沒有在任何地方定義,所以也不會工作。

+0

OP聲明[brand_list]是一個短代碼。這意味着'marque'即將來自短代碼 – 2015-02-05 23:47:33

+0

nook是正確的,商品標識來自brand_list短代碼。不管怎麼說,還是要謝謝你 – 2015-02-06 00:38:38