我有一个按钮,输入类型提交要使用MVC jquery验证表单

时间:2021-05-28 19:43:54

In my MVC4 application I have a input type submit button but I am preventing it's nature , e.preventDefault () in my jquery and making an AJAX POST call

在我的MVC4应用程序中,我有一个输入类型提交按钮,但我阻止它的性质,我的jquery中的e.preventDefault()并进行AJAX POST调用

('#submit').click(function(e){})

I want to validate whole form for required values before this call, and then only proceed further ..

我想在此调用之前验证所需值的整个表单,然后才进一步..

I am able to do this if I do not use this function ('#submit').click(function(e){}); ie. by using simple input submit button the form getting validated automatically by jquery ,

如果我不使用这个函数('#submit'),我可以这样做。单击(function(e){});即。通过使用简单的输入提交按钮,表格由jquery自动验证,

How can I use same functionality. in this case where I am making ajax call on submit button. I used this but it not working!!

我该如何使用相同的功能。在这种情况下,我在提交按钮上进行ajax调用。我用过这个但是没用!

$("#userform").validate();
$("#userform").valid()

1 个解决方案

#1


2  

Assuming you have jquery.unobtrusive and jquery.validate loaded then:

假设您已加载jquery.unobtrusive和jquery.validate,那么:

   $("#registerUserForm").submit(function () {
            if ($(this).valid()) {
                $.OverWatch.worker.postUserData(this.action, $(this).serialize(), function (data) {
                 //callback
                })
            }

            return false;
        })

    postUserData: function (url, data, callback) {

        $.LoadingOverlay("show");
        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            success: function (data) {

                if (callback) {
                    callback(data);
                    $.LoadingOverlay("hide");
                }

            },
            error: function (event, jqxhr, settings, thrownError) {
                $.LoadingOverlay("hide");
            }
        });

#1


2  

Assuming you have jquery.unobtrusive and jquery.validate loaded then:

假设您已加载jquery.unobtrusive和jquery.validate,那么:

   $("#registerUserForm").submit(function () {
            if ($(this).valid()) {
                $.OverWatch.worker.postUserData(this.action, $(this).serialize(), function (data) {
                 //callback
                })
            }

            return false;
        })

    postUserData: function (url, data, callback) {

        $.LoadingOverlay("show");
        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            success: function (data) {

                if (callback) {
                    callback(data);
                    $.LoadingOverlay("hide");
                }

            },
            error: function (event, jqxhr, settings, thrownError) {
                $.LoadingOverlay("hide");
            }
        });