当我选择一个选项时,代码js无法工作以分配输入类型编号 我的选择选项:
<div class="gjf2">
<div class="select" >
<?=$form->field($model, 'cat')->dropDownList($categoryFilters['cat']['options'],['class' => 'js-example-basic-single select-hidden'],['onchange'=>'myFunction()'],['id'=>'mySelect'],['options'=>['0' => ['disabled' => true],$select => ['selected' => true]]])->label(""); ?>
</div>
<p id="demo"></p>
</div>
我的js代码:
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = '<input type="number"/>';
}
答案 0 :(得分:0)
将onchange放入作为选项数组的第二个数组中,这意味着您需要这样做
<?=$form->field($model, 'cat')->dropDownList($categoryFilters['cat']['options'],['class' => 'js-example-basic-single select-hidden' ,'onchange'=>'myFunction()'])->label(""); ?>
请参考以下内容: https://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2
答案 1 :(得分:0)
DropDownList
应该是:
<?= $form->field($model, 'cat')->dropDownList(
$categoryFilters['cat']['options'],
[
'class' => 'js-example-basic-single select-hidden',
'onchange' => 'myFunction()',
'id' => 'mySelect',
'options' => ['0' => ['disabled' => true, 'selected' => true]],
])->label(false) ?>