dongyuan2388 2016-03-04 12:53
浏览 85
已采纳

Yii2输入字段集禁用依赖于其他字段

I have, a create form, where i ask two things. The first is a user_id the other is a name.

I want to achieve that, if the first is set then the other field will be disabled. And if because there i want to save that specific user's name. I tried it with javascript, but im so noob with js, thats why ask to you.

My codes is that:

    $script = <<<JS
        $('#contact-user_id').on('afterValidate', function (e) {
            if ( $('#contact-user_id').value.length > 0 ) {
                return document.getElementById("contact-name").disabled = true;
            }
        });
    JS;
    $this->registerJs($script);
    ?>


        <div class="row">
            <div class="col-md-6">
                <?= $form->field($model, 'user_id')->widget(Select2::className(), [
                    'value' => $model->user_id,
                    'data'=>ArrayHelper::map(User::find()->all(), 'user_id', 'name'),
                    'options'=>['placeholder'=>'Select User...'],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                ]) ?>

                <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

                <?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>
...
  • 写回答

1条回答 默认 最新

  • dongzongzhi6953 2016-03-04 13:10
    关注

    First of all, add the script at the bottom of your view or add it into the $(document).ready();

    Now, the code to change the name field depending upon user_id field.

    $('#contact-user_id').change(function(){
        if ($(this).val() != 0 || $(this).val() != '') {
           $('#contact-name').attr('disabled',true);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作