jquery submitHandler wp_ajax动作不起作用 - Wordpress

时间:2017-07-17 06:40:55

标签: jquery ajax wordpress

我正在验证然后提交值。

ajax动作在这里不起作用。

请查看代码。

请帮忙!!!!!!

function et_contact_form() {  ?>
<script type="text/javascript" >
jQuery(document).ready(function(jQuery){
 jQuery("#contact_modal").validate({
    rules: {
        name: "required",
        msg: "required"
    },
    messages: {
        name: "Please enter your name",
        msg: "Please enter a short message, what your inquiry is about"
    },
    submitHandler: function(form) {
        var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
        var name    = jQuery("#name").val();
        var data    = {
                   'action':'et_contact_modal',
                   'dname'  : name 
                  };

        jQuery.post(ajaxurl, data, function(response) {
            alert(response);
        });

    }

    });
  });

</script> <?php
}
add_action( 'wp_footer', 'et_contact_form' ); 

2 个答案:

答案 0 :(得分:0)

尝试以下代码以获取ajex通话功能

function et_contact_form() {  ?>
<script type="text/javascript" >
jQuery(document).ready(function ($) {
     jQuery("#contact_modal").validate({
        rules: {
            name: "required",
            msg: "required"
        },
        messages: {
            name: "Please enter your name",
            msg: "Please enter a short message, what your inquiry is about"
        },
        submitHandler: function(form) {
            var contact_modal_ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
            var name    = jQuery("#name").val();
            var contact_modal_str = '&action=et_contact_modal&dname='+name;
            jQuery.ajax({
                type: "POST",
                dataType: "html",
                url: contact_modal_ajaxurl,
                data: contact_modal_str,
                success: function(data){
                    var contact_modal_responsedata = jQuery(data);
                     alert(contact_modal_responsedata);

                },
                error : function(jqXHR, textStatus, errorThrown) {
                    $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }

            });
            return false;  // block the default submit action

        }

    });
});
</script> 
<?php
}
add_action( 'wp_footer', 'et_contact_form' ); 
?>

答案 1 :(得分:0)

发现问题!!

以上代码仅在管理员登录时有效,

add_action( 'wp_ajax_nopriv_et_contact_modal', 'et_contact_modal' );

如果管理员未登录,则添加操作

o