jQuery Validate - at least one from a group of checkboxes is checked only if a radio button is checked

后端 未结 2 1835
生来不讨喜
生来不讨喜 2021-01-13 06:50

I´m using jquery validate plugin to ensure at least one from a group of three checkboxes is checked and it´s woking fine. But I want validate these checkboxes only if the ra

2条回答
  •  忘掉有多难
    2021-01-13 07:25

    You simply need to alter the logic of your custom method.

    $.validator.addMethod('require-one', function(value) {
        if ($('#resp01-sim').is(':checked')) {
            return $('.require-one:checked').size() > 0;
        } else {
            return true;
        }
    }, 'Selecione pelo menos uma das opções.');
    

    DEMO: http://jsfiddle.net/TnmGr/4/

提交回复
热议问题