I would like to implement the solution of the question: jQuery - checkbox enable/disable in yii2, just reversed and combined with DB. Reversed means: I have some checkboxes and if the irep
checkbox is checked, all the other checkboxes should be disabled, else they should be enabled. So I have swapped $("input.irep").attr("disabled", true);
and $("input.irep").removeAttr("disabled");
.
irep
is stored in a db (as bool/tinyint value 1), and if I open the form, the fields that supposed to be disabled, are still going to be enabled, even if I'm adding 'disabled' => $model->irep
to it. If I remove the else it's almost OK, but unchecking doesn't re-enable the other checkboxes. Can you please point me to the right direction? Thank you.
Form:
$script = <<< JS
$(function() {
enable_cb();
$("#irep").click(enable_cb);
});
function enable_cb() {
if (this.checked) {
$("input.irep").attr("disabled", true);
} else {
$("input.irep").removeAttr("disabled");
}
}
JS;
$this->registerJs($script);
<?= $form->field($model, 'irep')->checkbox(['id' => 'irep']) ?>
<?= $form->field($model, 'intfg')->checkbox(['class' => ['irep'], 'disabled' => $model->irep]) ?>
<?= $form->field($model, 'extfg')->checkbox(['class' => ['irep'], 'disabled' => $model->irep]) ?>