donglie9067 2017-11-07 05:23
浏览 88
已采纳

如何在验证代码中成功添加ajax代码

how to add ajax code in success of validation code

<script type="text/javascript">
        $(document).ready(function () {

            $("#footer-request-form").validate({
                errorClass: 'has-error',
                success: "valid",

                errorPlacement: function (error, element) {
                    if(error.html()) error.appendTo(element.parent("div.form-group").addClass('has-error'));
                },

                unhighlight: function (element, errorClass, validClass) {
                    $(element).parents("div.form-group").removeClass('has-error');
                },
                success:function (label, element) {
                    $(element).removeClass("has-error");
                    $(element).parent().find('label.has-error').remove();
                },
                rules: {
                    name: {required: true},
                    phone: {phoneUS: true, required: true},
                    email: {required: true, email: true}
                },
                messages: {
                    phone: "Please enter a valid  phone number",
                    name: "Name is required!",
                    email: "Please enter a valid email address",
                }

            });
        });


    </script>

where to add ajax form on success of above code

jQuery.ajax({
            url: "contact_mail.php",
            data:'userName='+$("#name").val()+'&userEmail='+$("#email").val(),
            type: "POST",
            success:function(data){
            $("#mail-status").html(data);
            },
            error:function (){}
            });
        }
  • 写回答

1条回答 默认 最新

  • duanhong4274 2017-11-07 05:30
    关注

    try with submitHandler in jQuery

    $('#form').validate({
    
    ... your validation rules come here,
    
    submitHandler: function(form) {
        jQuery.ajax({
            url: "contact_mail.php",
            data:'userName='+$("#name").val()+'&userEmail='+$("#email").val(),
            type: "POST",
            success:function(data){
            $("#mail-status").html(data);
            },
            error:function (){}
            });
    }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!