【发布时间】:2020-03-09 15:58:42
【问题描述】:
所以,我对 wordpress 的 AJAX 处理程序有疑问。 这是我的表格:
form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter" name="filter">
<input type="hidden" name="action" value="myfilter">
<?php
if( $terms = get_terms( array( 'taxonomy' => 'category', 'orderby' => 'name' ) ) ) :
echo '<div class="d-flex justify-content-center">';
foreach ( $terms as $term ) :
if($first) : echo '<label><input type="radio" class="radio-formular" value="' . $term->term_id . '" name="categoryfilter" onclick="this.form.submit();" checked>' . $term->name.'</label>'; // ID of the category
else : echo '<label><input type="radio" class="radio-formular" value="' . $term->term_id . '" name="categoryfilter">' . $term->name. '</label>'; // ID of the category as the value
endif;
endforeach;
echo '</div>';
endif;
?>
<button id="sbm">Apply</button>
</form>
我希望,当我选择其中一个收音机时,表单将提交并由下一段代码处理:
<script type="text/javascript">
jQuery(function($){
$('#filter').submit(function(e){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
//filter.find('button').text('Processing...'); // changing the button label
},
success:function(data){
//filter.find('button').text('Apply filter'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
});
</script>
两个 sn-ps 都在同一个文件中。
functions.php
add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'] // ASC or DESC
);
// for taxonomies / categories
if( isset( $_POST['categoryfilter'] ) && $_POST['categoryfilter'] != '0' )
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
// if you want to use multiple checkboxed, just duplicate the above 5 lines for each checkbox
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo '<div class="container-apartament m-3 text-center">';
echo '<img src="'. wp_get_attachment_url(get_post_thumbnail_id($query->post->ID),'full') .'">';
echo '<div class="mt-2 mb-2 card-body">';
echo '<h4>' . $query->post->post_title . '</h4>';
echo '<a href="#myModal-'. $query->post->ID . '" class="band-text-inner"><img src="wp-content/uploads/2020/03/house-plan.png"> Plan </a>';
echo '<p class="card-price">'. get_field("price") .'€ no VAT</p>';
echo '<p class="card-yield">'. get_field("yield") .' NET <span class="text-mic-card">yield/yr: '. (string)0.05*get_field("price")*1000 .' € </span></p>';
echo '<a href="'. get_permalink() .'" class="invest-now">Invest Now </a>';
echo '</div>';
echo '</div>';
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
问题是: 通过按钮提交表单按预期工作,但是当我在单选按钮上设置 onclick 事件时,它卡在 admin-ajax.php 中而不是返回值。错误在哪里?自午餐以来我一直在努力解决这个问题,我似乎找不到解决方案。 我尝试过 preventDefault(),正如我在另一篇文章中看到的那样,但它阻止了表单提交,我根本没有得到任何回应。 对不起,我的英语不好和/或模棱两可的解释,提前感谢!
【问题讨论】:
-
"它卡在 admin-ajax.php 中而不是返回值" 调试模式是否开启?如果是,显示的错误是什么?
-
自定义字段键正确啊?
-
我认为自定义字段值错误?
-
当它到达 admin-ajax.php 时,它会在此处显示信息。我想这些字段没问题,因为当我通过按钮提交表单时一切正常。当我在另一个输入元素(如 radio 或 input type="submit" 上调用提交函数时出现问题
标签: javascript php jquery ajax wordpress