Ajax is not working in a loop

时间:2019-02-10 作者:Rafiq

我试图使用ajax显示与所选类别相关的数据。它仅适用于最后选定的术语,而不适用于所有选定的术语。任何帮助都将不胜感激。

var selected_cat = $(\'#my-categorychecklist input:checked\').map(function() {
    return this.value
}).get();

   $(document).ready( function () {
         var getid = $(\'#my-categorychecklist input:checked\').last().val();
         var matchid = $(\'#my-categorychecklist input:checked\').last().val();

         $(\'#section\').html(\'<div class="spinner"></div>\');
         var data = {
             \'action\': \'fields_selected\',
             \'post_id\': $(\'#fields-list-selected\').data(\'post_id\'),
             \'term_id\': selected_cat
         };

             $.post(ajaxurl, data, function (response) {
                     if(response != 0) {
                         $(\'.block\'+getid).html(response);
                         $(\'[data-toggle="tooltip"]\').tooltip();
                     } else {
                         $(\'.block\'+getid).html(" ");
                         $(\'[data-toggle="tooltip"]\').tooltip();
                     }
             });

     });///////PHP AJAX CALLBACK START
    $args = null;
    if (!empty($term_ids)){ //terms ids
        foreach($term_ids as $term_id){
            $args = array(
                \'post_type\'      => \'my_post\',
                \'posts_per_page\' => -1,
                \'meta_query\'    => array(
                    \'relation\' => \'AND\',
                    array(
                        \'key\'       => \'category\',
                        \'value\'     => $term_id,
                        \'compare\'   => \'EXISTS\',
                    ),
                    array(
                        \'key\'       => \'associate\',
                        \'value\'     => \'categories\',
                        \'compare\'   => \'LIKE\',
                    )
                )
            );
            $query = new WP_Query( $args );
            if ($query->have_posts()){

                // Start the Loop
                global $post;
                // Process output
                ob_start();

                include MY_TEMPLATES_DIR . \'custom-field.php\';
                wp_reset_postdata(); // Restore global post data stomped by the_post()
                $output = ob_get_clean();

                print $output;

                if( $ajax ) {
                    wp_die();
                }
            } else{
                // Process empty output
                ob_start();
                ?>
                <?php
                $output = ob_get_clean();
                print $output;
                //print "No data found !";
            }
        }
    }/*AJAX CALLBACK END*/if ($term_id_selected) {
        foreach ($term_id_selected as $single_term){
        ?>
        <div id="fields-list-selected" data-post_id="<?php echo $post_ID; ?>">
            <?php
            do_action(\'wp_ajax_fields_selected\', $post_ID, $single_term); ?>
        </div>
        <?php
    }}

1 个回复
SO网友:user206904

您的代码

 var getid = $(\'#my-categorychecklist input:checked\').last().val();
请参见。last(),这就是为什么您只获取最后一个项。另外,为什么在getid和matchid中有相同的内容?

您需要将所选类别存储在一个数组中,并在其中循环,请尝试以下操作:

var selected_cats = $(\'#my-categorychecklist input:checked\').map(function() {
    return this.value
})
(没有.get())然后使用

    selected_cats.forEach(function(element) {
        console.log(element)
      //make sure element has what you\'re expecting then do you something on element
}
试着做控制台。在每个变量上记录()并在控制台中查看这些变量是否具有您期望的结果。确保selected\\u cats具有选定类别的数组