Using depends with the jQuery Validation plugin

前端 未结 8 553
[愿得一人]
[愿得一人] 2021-01-12 11:52

I\'ve got a form with a bunch of textboxes that are disabled by default, then enabled by use of a checkbox next to each one.

When enabled, the values in these textbo

8条回答
  •  春和景丽
    2021-01-12 12:45

    Following @Collin Allen answer:

    The problem is that if you uncheck a checkbox when it's error message is visible, the error message doesn't go away.

    I have solved it by removing the error message when disabling the field.

    Take Collin's demo and make the following changes to the enable/disable process:

    jQuery('#ItemList :checkbox').click(function()
    {
        var jqTxb = $(this).siblings(':text')
        if ($(this).attr('checked'))
        {       
            jqTxb.removeAttr('disabled').focus();
        }
        else
        {
            jqTxb.attr('disabled', 'disabled').val('');
            var obj = getErrorMsgObj(jqTxb, "");
            jqTxb.closest("form").validate().showErrors(obj);
        }
    });
    function getErrorMsgObj(jqField, msg)
    {
        var obj = {};
        var nameOfField = jqField.attr("name");
        obj[nameOfField] = msg;
        return obj;
    }
    

    You can see I guts remove the error message from the field when disabling it

    And if you are worrying about $("form").validate(), Don't! It doesn't revalidate the form it just returns the API object of the jQuery validation.

提交回复
热议问题