Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Wordpress AJAX提交结果在admin-AJAX.php中打开,而不是在content div中打开_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript Wordpress AJAX提交结果在admin-AJAX.php中打开,而不是在content div中打开

Javascript Wordpress AJAX提交结果在admin-AJAX.php中打开,而不是在content div中打开,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,因此,我对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

因此,我对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>
这两个代码段位于同一个文件中

functions.php


问题是:通过按钮提交表单的工作方式与预期的一样,但是当我在单选按钮上设置onclick事件时,它会卡在admin-ajax.php中,而不是返回值。错在哪里?午饭后我一直在努力解决这个问题,似乎找不到解决办法。 正如我在这里的另一篇文章中看到的那样,我尝试了默认设置,但是它阻止了表单的提交,我没有得到任何回应。
对不起,我的英语不好和/或含糊不清的解释,并提前感谢

我为这些家伙找到了解决办法。我设法添加了一个onclick事件,该事件触发了对按钮的单击,从而使一切正常工作。尽管如此,通过字段提交表单仍然是不可能的,如果我找到了,我会在这里发布。

这里有一个片段,是我认为您正在尝试的内容的骨架:

阻止表单实际提交,以便不加载新页面 在提交表单时执行默认操作(例如,它只是将日志记录到控制台),但也可能是Ajax请求 表单值更改时触发非默认操作 jQueryfunction${ $'mainForm'。在'submit'上,functionevent{ event.preventDefault //当表单提交到这里时,可以执行任何您想执行的操作,例如ajax请求 //在当前上下文中,这指的是表单html对象,而不是jquery对象 console.info'Running submit action.Form data:',$this.serialize }.on'change',functionevent{ //在表单中的任何内容发送“更改”操作时触发提交。 //如果需要,还可以将其筛选到表单中的特定项 控制台。警告“表单已更改” //使用jquery触发提交事件 //在当前上下文中,这指的是表单html对象,而不是jquery对象 //请注意,如果执行“this.submit”,则表单将提交,并且不会调用提交处理程序 $this.trigger'submit' } } A. B
它被困在admin-ajax.php中,而不是返回调试模式启用时的值?如果是,显示的错误是什么?自定义字段键正确吗?我认为自定义字段值错误?当它进入admin-ajax.php时,它会在这里显示信息。我想字段是可以的,因为当我通过按钮提交表单时,一切都可以。当我在另一个输入元素(如radio或input type=submit)上调用submit函数时,就会出现问题
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();
}