Calling blockUI and unblockUI in combination with jQuery validator plugin

只谈情不闲聊 提交于 2020-01-25 05:10:20

问题


I have a very complex form with the validation working correctly. However, since it can take awhile for the validation to complete, I'd like to use blockUI to be called when I click the form's submit button to prevent confusion and double-submissions. I can't quite figure out how to do this.

My code looks like this:

$("#credential").validate({
     rules: {
              EngId: {
                 required: true
                 }
             ClientAccount: {
                 required: true
                 }
              ...
        }

and I'm calling the validation with several buttons (using their click function) depending on selections in the form, often disabling some of the rules:

$("#buttonname").click(function() {
   $("#fieldname").rules("remove");
   ...
   $("#credential").submit();
});

What I can't figure out is where the blockui and unblockui calls would go so that when the user clicks the button, before validation starts, blockui does its magic, and if the validation finds a problem, unblockui is called and enables the form again.

I'm pretty new to Jquery and I can't find any examples that I've been able to implement successfully. I would appreciate any help anyone could give (please excuse if this has been covered before).


回答1:


$(document).ready(function() {
    $('#form1').validate({
        rules: {
            fieldone: { required: true },
            fieldtwo: { required: true }
        },
        submitHandler: function(form) {
            $(form).block();
            form.submit();
        }
    });

    $('input:checkbox[name=toggleone]').click(function() {
        if ($(this).is(':checked')) {
            $('input[name=fieldone]').rules('add', { required: true });
        } else {
            $('input[name=fieldone]').rules('remove');
        }
    });

    $('#altsubmit').click(function() {
        $('input[name=fieldtwo]').rules('remove');
        $('#form1').submit();
    });
});

I put together a page to demonstrate a working form with validate, blockui, and dynamic rules.



来源:https://stackoverflow.com/questions/2346360/calling-blockui-and-unblockui-in-combination-with-jquery-validator-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!