jquery ajax在没有加载页面的情况下无法正常工作

时间:2018-03-19 11:07:16

标签: javascript php jquery ajax wordpress

`

$ previous_year = $ year = 0;

$ previous_month = $ month = 0;

$ ul_open = false;

$ myposts = get_posts(array(

'numberposts'=> $ numposts,

'orderby'=> 'post_date',

'order'=> 'DESC'

));

foreach($ myposts as $ post):?>

    $ year = mysql2date('Y',$ post-> post_date);
$ month = mysql2date('n',$ post-> post_date);
$ day = mysql2date('j',$ post-> post_date);
?>

 

的http://本地主机/ klientasnew /档案/ = 01" >


jQuery(document).ready(function(){
jQuery('#df')。on('click',function(e){
e.preventDefault();
var rml_post_id = jQuery(this).data('href');
jQuery.ajax({
url:'/ archives?=',
类型:'post',
数据:{
post_id:rml_post_id
},
成功:功能(反应){
//jQuery('#dd').html(response);
警报(响应);
}
});
jQuery的(本).hide();
});
});

`

2 个答案:

答案 0 :(得分:0)

试试此代码

    <?php $numposts = -1;

$previous_year = $year = 0;

$previous_month = $month = 0;

$ul_open = false;

$myposts = get_posts(array(

'numberposts' =>    $numposts,


'orderby'   =>    'post_date',  


'order' => 'DESC'  


));              

?>               

<?php foreach($myposts as $post) : ?>

<?php setup_postdata($post);

$year =    mysql2date('Y', $post->post_date);

$month = mysql2date('n', $post->post_date);

$day = mysql2date('j', $post->post_date);

?>

<?php if ($year != $previous_year || $month != $previous_month) : ?>

<?php if ($ul_open == true) : ?>

<?php endif; ?>

<li><a id="df" rel="<?php echo get_the_date('m'); ?>" href="<?php echo 
site_url();?>/archives?=<?php echo get_the_date('m'); ?>" onclick="return loadmore();">

<span><?php the_time('F Y'); ?></span></a>

</li>

<?php $ul_open = true; ?>

<?php endif; ?>

<?php $previous_year = $year; $previous_month = $month; ?>

    <script type="text/javascript">

    function loadmore(){ 

        e.preventDefault();

        var rml_post_id = jQuery(this).data( 'href' ); 

        jQuery.ajax({

            url : rml_post_id,

            type : 'post',

            data : {

            post_id : rml_post_id

            },

            success : function( response ) {

            //jQuery('#dd').html(response);
            alert(response);
            return false;

            }

        });

        jQuery(this).hide();            

     }  

    </script>
<?php endforeach; ?>

答案 1 :(得分:0)

<?php $numposts = -1; <br>

$ previous_year = $ year = 0;

$ previous_month = $ month = 0;

$ ul_open = false;

$ myposts = get_posts(array(

'numberposts'=&gt; $ numposts,

'orderby'=&gt; 'POST_DATE',

'order'=&gt; 'DESC'

));?&GT;


$ year = mysql2date('Y',$ post-&gt; post_date);

$ month = mysql2date('n',$ post-&gt; post_date);

$ day = mysql2date('j',$ post-&gt; post_date);?&gt;




  • “&gt;






  • $(文件)。就绪(函数(){
        $( “get_project”)。点击(函数(){
            var id = $(this).attr(“data_id”);
            var data ='?='+ encodeURIComponent(id); //这是我使用'&amp;添加多个数据的地方“
            //警报(数据);
            $阿贾克斯({
                类型: “GET”,
                url:“/ archives”+ data,
                成功:功能(html){
                $( “BLG-BCK。”)HTML(HTML);
            }
        });
          });
    });