我有一个radioList
,带有2个值1和2。
如果收音机设置为2,我想隐藏textInput
。
我尝试过:
$script = <<< JS
$(document).ready(function () {
if(document.getElementById('radio').val() == '2' ) {
document.getElementById('textInput').style.display = 'none';
document.getElementById('textInput').hide();
} else {
document.getElementById('textInput').show();
}
});
JS;
$this->registerJs($script);
echo $form->field($model, 'radio')->radioList(['1' => '1', '2' => '2'], ['id' => 'radio']);
echo $form->field($model, 'textInput')->textInput(['id' => 'textInput']);
有什么想法吗?
答案 0 :(得分:1)
您的代码存在多个问题
$this->registerJs($script,\yii\web\View::POS_READY)
加载/注册脚本。.val()
时,没有任何名称为.value
的方法,而是document.getElementById()
。.show()
和.hide()
radio
中提供的ID radioList
将是所有无线电列表输入所在的容器的id
。由于您使用了javascript标签,因此我正在使用addEventListner
绑定事件,请参见下面的代码并将其添加到视图顶部。
$script = <<< JS
document.querySelectorAll("#radio input[type='radio']").forEach(function(element){
element.addEventListener('click',function(){
if(this.value == '2' ) {
document.querySelector('#textInput').parentElement.style.display = 'none';
} else {
document.querySelector('#textInput').parentElement.style.display = 'block';
}
});
});
JS;
$this->registerJs($script, \yii\web\View::POS_READY);