【问题标题】:jQuery $.ajax function doesn't get data from $(...).validate functionjQuery $.ajax 函数不从 $(...).validate 函数获取数据
【发布时间】:2013-03-03 15:36:03
【问题描述】:

在尝试结合 jQuery 验证(插件)和使用 ajax 将数据发送到服务器时,我遇到了以下问题。

如果我使用这个 jQuery 方法仅将数据单独发布到服务器,那么一切正常。

function doAjaxPost() {
    // get the form values
    var name = $('#name').val();
    var pswd = $('#pswd').val();

    $.ajax({
    type: "POST",
    url: "${pageContext. request. contextPath}/loginUser.htm",
    data: "name=" + name + "&pswd=" + pswd,

    success: function(response){    
        // we have the response
        if(response.status == "OK") {
            $('#info').html("User has been added to the list successfully.<br>"+
                                "The User Details are as follows : <br> Name : "+ 
                                response.result.name + " <br> Password: " + response.result.pswd);
            $('#name').val('');
            $('#pswd').val(''); 
        } else {
            $('#info').html("Sorry, there is something wrong with the data provided")
        }
    },
    error: function(e){
        alert('Error: ' + e);
    }
    });

}// end of doAjaxPost

但是当我尝试使用 jquery.validate.min.js 验证用户的输入时,服务器的响应显示一切正常,但名称和密码的值未定义我怀疑我的方法 doAjaxPost() 在 jquery.validator 之后无法从表单中提取数据。 这是我的验证功能:

$(document).ready(function(){
        $("#loginform").validate({

            submitHandler: function(form) {
                doAjaxPost();
            },

            rules:{
                name:{
                    required: true,
                    minlength: 4,
                    maxlength: 16,
                },
                pswd:{
                    required: true,
                    minlength: 6,
                    maxlength: 16,
                }, 

           },
           messages:{
                name:{
                    required: "Login - is a mandatory field",
                    minlength: "Name should contain minimum {0} symbols",
                    maxlength: "Maximum symbols - {0}",
                },
                pswd:{
                    required: "Password - is a mandatory field",
                    minlength: "Password should contain minimum {0} symbols",
                    maxlength: "Password should contain maximum {0} symbols",
                },

           },

        });

    });

所以我的问题是如何从经过验证的表单中提取数据并将其传递给 doAjaxPost 函数?

因为我认为这些变量从表单中得不到任何东西

var name = $('#name').val();
var pswd = $('#pswd').val();

也许我应该使用像 $('#loginform #name') 这样的选择器?我试过了,它不起作用。请帮忙。

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:

    引用 OP:

    “所以我的问题是如何从经过验证的表单中提取数据并传递它 做AjaxPost功能?”

    Use jQuery .serialize() 并将您的ajax 放入插件的submitHandler 中。

    As per the documentation for the Validate pluginsubmitHandler的回调函数为:

    "表单有效时处理实际提交的回调。获取 形式作为唯一的论据。替换默认提交。 权利 验证后通过 Ajax 提交表单的地方。”

    假设您的 ajax 函数是正确的,请尝试此代码:

    $(document).ready(function () {
    
        $('#myform').validate({ // initialize the plugin
            // your rules & options,
            submitHandler: function (form) {
                //var name = $('#name').val(); // use serialize
                //var pswd = $('#pswd').val(); // use serialize
                $.ajax({
                    type: "POST",
                    url: "${pageContext. request. contextPath}/loginUser.htm",
                    //data: "name=" + name + "&pswd=" + pswd,  // use serialize
                    data: $(form).serialize(),
                    success: function (response) {
                        // we have the response
                        if (response.status == "OK") {
                            $('#info').html("User has been added to the list successfully.<br>" +
                                "The User Details are as follows : <br> Name : " + response.result.name + " <br> Password: " + response.result.pswd);
                            $('#name').val('');
                            $('#pswd').val('');
                        } else {
                            $('#info').html("Sorry, there is something wrong with the data provided")
                        }
                    },
                    error: function (e) {
                        alert('Error: ' + e);
                    }
                });
                return false; // blocks redirect after submission via ajax
            }
        });
    
    });
    

    演示:http://jsfiddle.net/Cdvqw/

    编辑:OP 已经从插件的submitHandler 中调用了他的外部ajax 函数。

    【讨论】:

    • 您对表单序列化是绝对正确的。我添加了这个变量 var queryString = $('#loginform').formSerialize();并将其用作我的数据,一切都很好。我认为,序列化数据是有意义的。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2014-07-12
    • 2015-05-09
    相关资源
    最近更新 更多