2017-08-24 12 views
1

私は混乱したので、私のタイトルのqustionは良くない場合は申し訳ありません。どのようにAJAXの成功のすべてのjQueryボタンをonclickしないでください

ケース: 私はFacebookのようなポストタイムラインのループを作成し、各投稿にコメントがあります。このような構造:

++++++++++++++++++++++++++

ポスト1#ID-321 てきた__ 10件のコメント(show_again_321)< - [このボタンloadmoreコメント、2を表示 -

ポスト2#ID-876 __ 17件のコメント(show_again_876)<あり[このボタンloadmoreコメント、各クリックの2のコメントを表示します]クリックごとのコメント]

ポストのn .....

+++++++++++++++++++++++++

私はワードプレスでこのコードを作ります。

このような
// Load more reply 
     var cur_page = 1; 

     $(document).on('click', 'button[class*="show_again_"]', function() { 
      var reply_id = $(this).attr('reply-id'); 

      var reply_data = { 
       action: 'ajax_replyto_loadmore', 
       reply_id: reply_id, 
       cur_page: cur_page, 
      } 

      $.ajax({ 
       url: ajaxurl, 
       type: 'POST', 
       data: reply_data, 
       dataType: 'json', 
       success: function(response, result, xhr) { 
        console.log(response); 
        cur_page++; 
       } 
      }); 
     }); 

と機能:今

function ajax_replyto_loadmore() { 
    global $wpdb, $post; 

    $reply_to = '_bbp_reply_to'; 
    $paged = esc_attr($_POST['cur_page'] + 1); 
    $reply_id = esc_attr($_POST['reply_id']); 
    $post_reply = bbp_get_reply_post_type(); 

    $get_reply_to = $wpdb->get_results($wpdb->prepare(" 
     SELECT * 
     FROM $wpdb->postmeta 
     WHERE meta_key = %s 
     AND meta_value IN ($reply_id) 
     ", 
     $reply_to 
    )); 

    foreach ($get_reply_to as $key => $value) { 
     $list_reply_to[] = $value->post_id; 
    } 

    $args_to = array(
     'post__in' => $list_reply_to, 
     'posts_per_page' => 1, 
     'post_type' => $post_reply, 
     'post_status' => array('publish', 'future'), 
     'paged' => $paged, 
    ); 

    $the_query_to = new WP_Query($args_to); 

    if ($the_query_to->have_posts()) : 
     while ($the_query_to->have_posts()) : $the_query_to->the_post(); 
      $article[] = '<li>' . bbp_get_reply_content(get_the_ID()) . '</li>'; 
     endwhile; 
    endif; 

    $datareply = array(
     'xxx' => 'yyy', 
     'paged' => $paged, 
     'get_reply_to' => $article, 
     'reply_id' => $reply_id, 
    ); 

    wp_send_json($datareply); 

    // reset postdata 
    wp_reset_postdata(); 
    wp_die(); 
} 

、ボタンshow_again_xxxは、彼らがページング$を合計retrunクリックした場合、

ケース:。

  • ボタンは、[リターン$をクリックしshow_again_321ページ番号= 2]
  • ボタンshow_again_876クリック[retur N $は、ページング= 3]私はそれを望んでいない

、私は、各ボタンのshow_again_xxxは、彼らがページングリターンを$を合計しません。、なめたい

このコンソールログの結果

enter image description here

あなたは私の問題を理解していれば

、私を助けて。..してください.....

更新ソリューション:

// Load more reply 
     // var cur_page = 1; 

     $(document).on('click', 'button[class*="reply_loadmore_"]', function(e) { 
      var cur_page = $(this).attr('cur-page'); 
      var reply_id = $(this).attr('reply-id'); 

      var reply_data = { 
       action: 'ajax_replyto_loadmore', 
       reply_id: reply_id, 
       cur_page: cur_page, 
      } 

      $.ajax({ 
       url: ajaxurl, 
       type: 'POST', 
       data: reply_data, 
       dataType: 'json', 
       success: function(response, result, xhr) { 
        console.log(response); 
        $('.reply_loadmore_' + reply_id).attr('cur-page', response.paged); 
        cur_page++; 
       } 
      }); 
     }); 

答えて

1

reply_idのように、隠れた入力のようにHTMLコンテンツ内にページングを設定します。その後、応答時に応答$pagedの値で更新します。また、あなたがこのようなvar cur_page = 1;

何かを削除する必要があります:

//ロードがより

$(document).on('click', 'button[class*="show_again_"]', function() { 
     var reply_id = $(this).attr('reply-id'); 
     var cur_page = $(this).attr('cur_page'); 


     var reply_data = { 
      action: 'ajax_replyto_loadmore', 
      reply_id: reply_id, 
      cur_page: cur_page, 
     } 

     $.ajax({ 
      url: ajaxurl, 
      type: 'POST', 
      data: reply_data, 
      dataType: 'json', 
      success: function(response, result, xhr) { 
       console.log(response); 
       cur_page++; 

      } 
     }); 
    }); 
+0

感謝を返信、完璧に動作します。私の更新の質問を参照してください。 – Opsional

+0

いいですね。すばらしいです。 – mokiSRB

関連する問題