jQuery turn off form validation

本小妞迷上赌 提交于 2020-01-11 10:43:15

问题


Im using the bog standard form validation plugin. I have two radio buttons 'yes' 'no' and what im trying to do is set validation if a radio 'yes' is checked... Which works, but i need to then turn off validation if the 'no' radio is selected. Im trying the following...

<script type="text/javascript">
    $(document).ready(function()
    {
        $('.cbyes').click(function() 
        {
            $('.hiddenDiv').show('slow');
            $("#regForm").validate();
        });
        $('.cbno').click(function() {
            $('.hiddenDiv').hide('slow');
            $('#regForm')[0].reset();
        });
    });
</script>

回答1:


Assuming there's no practical way to "turn off" validation once it's been applied to a form, can you use the validate method, but set it ignore everything in the form?

$(document).ready(function()
{
    $('.cbyes').click(function() 
    {
        $('.hiddenDiv').show('slow');
        $("#regForm").validate();
    });
    $('.cbno').click(function() {
        $('.hiddenDiv').hide('slow');
        $('#regForm')[0].reset();
        $('#regForm').validate({
            ignore: "#regForm *"
        });
    });
});

This example uses a dumb universal selector that should be replaced with something more appropriate for your particular form.



来源:https://stackoverflow.com/questions/1980622/jquery-turn-off-form-validation

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