检索下拉我有这个表: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过滤,也许我没有正确地使用它。
您可以在浏览器中查看开发者控制台,看到很多情况。具体来说,你可以看到网络调用和响应,看看页面发送到服务器和服务器发回的数据/信息 – 2015-02-05 23:41:21
(array('id'=>'arome $ id'是错的,你不是正确地转义字符串try(array(“id”=>“arome $ id” – 2015-02-05 23:51:47
我对'id'进行了修改,但它仍然无法正常工作,当我查看控制台时,我看到了对admin-ajax的调用。 PHP的200返回码,还有什么我可以看看? – 2015-02-06 00:38:02