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

后端 未结 2 1826
生来不讨喜
生来不讨喜 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:15

    You could try the built-in functionality:

    $( "#itemForm" ).validate({
      rules: {
        opt01: {
          required: "#resp01-sim"
        }
      }
    });
    

    http://jqueryvalidation.org/required-method/#example:-makes-details-required-only-if-#other-is-checked

    0 讨论(0)
  • 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/

    0 讨论(0)
提交回复
热议问题