我在贴子元中有一个用于贴子的数组。我希望能够列出管理区域中数组的每个单独元素,并从该数组中删除一个值。
当前,我循环遍历每个数组元素,并尝试使用ajax删除它们,当我单击delete按钮时,它会将值发送到我的ajax操作,然后加载所需的post meta,删除该值,然后更新post meta。然而,我的控制台。日志(数据)每次都会返回0,并且不会使div褪色。有人知道为什么吗?
function print_pdfs_attached() {
global $post;
$attached_pdfs = get_post_meta( $post->ID, \'pdf_upload_url\', true ); ?>
<p>All PDFs attached to the article</p>
<?php
$i=0;
if($attached_pdfs) {
foreach($attached_pdfs as $pdf) { ?>
<div id=<?php echo $pdf; ?>>
<p><?php echo $pdf; ?></p>
<input type="hidden" value="<?php echo $pdf; ?>" id="ajaxtestdel_postid<?php echo $i ?>">
<tr>
<th scope="row"><label for="del">delete</label></th>
<td><input type="button" name="del" id="del<?php echo $i; ?>" value="delete" class="button"></td>
</tr>
<script>
jQuery(\'#del<?php echo $i; ?>\').on(\'click\', function(){
var post = jQuery(\'#ajaxtestdel_postid<?php echo $i ?>\').val(); // get post id from hidded field
console.log(post);
jQuery.ajax({
url: \'admin-ajax.php\', // in backend you should pass the ajax url using this variable
type: \'POST\',
data: { action : \'ajaxtestdel\', postid: post },
success: function(data){
console.log(data);
jQuery("#<?php echo $pdf; ?>").fadeOut( "slow" );
}
});
});
</script>
<?php
$i++;
?>
</div>
<?php
}
}
else { ?>
<p>No PDFs are currently attached to this article</p> <?php
}
}
function ajaxtestdel(){
global $post;
$currentPdfs = get_post_meta( $post->ID, \'pdf_upload_url\', true );
$postValue = isset($_POST[\'postid\']) ? $_POST[\'postid\'] : \'\';
$array_without_strawberries = array_diff($currentPdfs, array($postValue));
update_post_meta( $post->ID, \'pdf_upload_url\', $array_without_strawberries );
}
add_action(\'wp_ajax_ajaxtestdel\', \'ajaxtestdel\');
SO网友:Welcher
您没有打印AJAX回调中的任何内容。正在尝试使用wp_send_json_success:
function ajaxtestdel(){
global $post;
$currentPdfs = get_post_meta( $post->ID, \'pdf_upload_url\', true );
$postValue = isset($_POST[\'postid\']) ? $_POST[\'postid\'] : \'\';
$array_without_strawberries = array_diff($currentPdfs, array($postValue));
update_post_meta( $post->ID, \'pdf_upload_url\', $array_without_strawberries );
// Send something back to AJAX
wp_send_json_success();
}
我有一种感觉,也许您的jQuery选择器对于淡出是不正确的-
jQuery("#<?php echo $pdf; ?>")
什么是
$pdf
它是有效的id名称吗?
希望这有帮助!