简体   繁体   中英

How to show/hide an element on checkbox checked/unchecked states using jQuery

I have this code:

<div class="form-group">
                        <?= $form->labelEx($model, 'd_options', array('class' => 'col-xs-12 col-sm-4 control-label')) ?>
                        <div class="col-xs-12 col-sm-8" id="d_options_element">
                            <?= $form->checkBoxList($model, 'd_options', $model->getData('d_options'), array(
                                'template' => '<div  class="checkbox col-xs-12 col-sm-4">{beginLabel}{input}{label}{endLabel}</div>',
                                'separator' => '',
                            )); ?>
                            <div class="col-xs-12 col-sm-8 p_16_13" id="d-parts"
                                 style="<?= (in_array(12, $model->d_options)) ? '' : 'display: none;' ?>">
                                <?= $form->textField($model, 'p_16_13', array(
                                    'class' => 'form-control dotted', 'placeholder' => Yii::t('subscription', 'указать другой вид деятельностит'))) ?>
                                <?= $form->error($model, 'p_16_13') ?>
                            </div>
                            <?= $form->error($model, 'p_16') ?>
                        </div>
                    </div>

getData retrieve elements of checkbox: this code is getData code:

 $data = array(
            'b_category' => array(
               '1', 
               '2',
               '3',
               'other'   
                      ),
);
        return $data[$property];
    }

I could not develop js file that responsible for showing/hiding following textField:

<?= $form->textField($model, 'p_16_13', array(
                                    'class' => 'form-control dotted', 'placeholder' => Yii::t('subscription', 'указать другой вид деятельностит'))) ?>

only when user presses 'other' button.(getData array). How can i develop it?

$(selector).on('change', function(){
    if($(this).is(':checked')){
        // checkbox is checked
    } else {
        // here is not
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM