首页 文章

Wordpress - 带有ajax的表单

提问于
浏览
0

我想在Wordpress中使用复选框进行过滤 . 我首先想要实现的是测试它并使用ajax在表单中的复选框单击时发送变量 .

这是带有复选框的表单:

<form  method="post" id="filter">
<input type="checkbox" name="f" value="1" onchange="this.form.submit()" <?php if  ($face =='1') {echo 'checked';}?>
<input type="checkbox" name="t" value="1" onchange="this.form.submit()" <?php if ($twitter=='1') {echo 'checked';}?>    
<input type="hidden" value="myfilter"> 
</form>

这是javascript:

jQuery(document).ready( function(){  
    jQuery('#content').on('submit', '#filter', function(e) {
        e.preventDefault();     
        var test = jQuery('#test-btn').data( 'id' );

        jQuery.ajax({           
            url : rml_obj.ajax_url,
            type : 'post',
            data : {
                action : 'test_function',
                security : rml_obj.check_nonce,
                test_data : test
            },
            success : function( response ) {
                alert (test);               
                jQuery('#result').html(response);

            }
        }); 
    });
});

这是functions.php中的函数:

function test_function() {  
    check_ajax_referer( 'rml-nonce', 'security' );  
    $test_data = $_POST['test_data'];
    echo $test_data; 
    die();
}
add_action('wp_ajax_test_function', 'test_function'); 
add_action('wp_ajax_nopriv_test_function', 'test_function');

现在我卡住了.2519576_ . 如果我添加这一行来形成动作 action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" ,那么在提交时我就停留在ajax url:admin-ajax.php . 如果我从表单中删除操作行,那么页面简单重新加载 .

当我使用click元素时,而不是表单提交我成功了 . 但是我需要让它在表单提交上工作,这样我就可以通过单击各种复选框来实现过滤 .

如果有人能告诉我我做错了什么,我会在此基础上构建并实际从表单中发送复选框值并在函数中执行查询以过滤数据 .

1 回答

  • 0

    问题形式是 onchange="this.form.submit()" . 删除该部分后,我能够继续 .

相关问题