开发者

Reset required fields - jQuery

I have some fields that I'm requiring depending on which button is clicked. The behavior I'm seeing though is not what I would expect nor desire. When I click one of the buttons, it sets the required fields as I'd like, but then if I click another button, it doesn't reset them for some reason.

Code is below. Thanks for any help you can provide:

function makeAllRequired() {
        $("#SomeForm").validate({
            rules: {
                StartDate: {
                    required: true,
                    date: true
                },
                Name: {
                    required: true
                }

            },
            errorElement: "div"
        });
    }

    function makeSomeRequired() {
        $("#SomeForm").validate({
            rules: {
                StartDate: {
                    required: true,
                    date: true
                }
            },
            errorElement: "div"
        });
    }

    $(document).ready(function () {
        $("#SomeButtonOne").click(funct开发者_如何学编程ion () {
            makeAllRequired();
            $("#SomeForm").attr("action", "/here/there");
            $("#SomeForm").submit();
        });
$("#SomeButtonTwo").click(function () {
            makeSomeRequired();
            $("#SomeForm").attr("action", "/here/elsewhere");
            $("#SomeForm").submit();
        });
});


Try reseting the form before setting new required items: http://docs.jquery.com/Plugins/Validation/Validator/resetForm


I'm not 100% sure but try removing the rules first before you apply new ones

function makeAllRequired() {
    $("#SomeForm").rules("remove");
    $("#SomeForm").validate({
        rules: {
            StartDate: {
                required: true,
                date: true
            },
            Name: {
                required: true
            }

        },
        errorElement: "div"
    });
}

function makeSomeRequired() {
    $("#SomeForm").rules("remove");
    $("#SomeForm").validate({
        rules: {
            StartDate: {
                required: true,
                date: true
            }
        },
        errorElement: "div"
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜