一旦我按下单选按钮,我就会尝试运行一个php循环。我意识到我必须使用 ajax 来做到这一点。我对 ajax 很陌生,所以我很难完成这项工作。现在发生的事情是,我什至无法再按下单选按钮,什么也没出现。
到目前为止我尝试过:
我的循环(工作正常):杂志.php:
<?php
$args = array( 'post_type' => 'magazine', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
}
?>
Ajax 在主页:
$('radio').click(function() {
$.ajax({
url: 'magazines.php',
success: function(){
alert('success');
}
});
return false;
});
任何帮助将不胜感激。谢谢!
radio
是一个类型选择器,它匹配<radio>
你没有的元素(HTML 中不存在的元素)。
你需要编写一个真正匹配你的元素的选择器,例如 input
.
试试这个
$('body').on('click','input:radio',function() {
$.ajax({
url: 'magazines.php',
success: function(data){
$('#your-selector').html(data);
}
});
})
试试这个:它的测试
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
$(function(){
$('input:radio').click(function() {
console.log("clicked");
$.ajax({
url: 'magazines.php',
success: function(response){
console.log(response);
alert('success');
}
});
return false;
});
});
</script>
请尝试这个
$('radio').click(function() {
$.get('magazines.php', {}, function(){
alert('success');
});
});