onchange js无法在yii2 php select中工作

时间:2018-11-27 09:20:16

标签: javascript yii2-advanced-app

当我选择一个选项时,代码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"/>'; }

2 个答案:

答案 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) ?>