2017-02-24 79 views
0

我使用yii2动态窗体wbraganca创建动态rows.This工作正常。 我有一个类别下拉的形式,其中onchange im根据下拉的值加载a。如果下拉列表中的值是其他值?然后im加载这个div有一个文本框,用户可以输入他的其他类别。Yii2动态窗体wbrganca onchange不会wodrking

这个onchange在第一行工作正常,当我点击新行时,会生成一个新行,但是在另一个选项不会加载文本框。

下面是我的表单代码和java脚本代码。

 <?= $form->field($bill, "[{$index}]category")->dropDownList(['Grocery' =>'Grocery', 'Power' => 'Power', 'Electricity' => 'Electricity', 'Water' => 'Water','others'=>'others'],['prompt'=>'Select...','onchange' => 'return showout(this.value)'])->label(false); ?> 

股利这是对变化而装入

<div id="mydiv" style="display: none;width: 88px;"> 
    <?= $form->field($bill, "[{$index}]other_category")->textInput(['maxlength' => true]) ?> 
    </div> 

的Java脚本代码

<script type="text/javascript"> 
    function showout(a) 
{ 
    if (a == 'others') { 
     $('#mydiv').show(); 
    } else { 
     $('#mydiv').hide(); 
    } 
} 
</script> 

回答

0

使用class,而不是id

<?= $form->field($bill, "[{$index}]category")->dropDownList(['Grocery' =>'Grocery', 'Power' => 'Power', 'Electricity' => 'Electricity', 'Water' => 'Water','others'=>'others'],['prompt'=>'Select...', 'onchange' => 'showout($(this))'])->label(false); ?> 
<div class="mydiv" style="display: none;width: 88px;"> 
    <?= $form->field($bill, "[{$index}]other_category")->textInput(['maxlength' => true]) ?> 
</div> 

JS

function showout(category) 
{ 
    var index = category.attr("id").replace(/[^0-9.]/g, ""); 
    if (category.val() == 'others') { 
     $("#bill-"+index.concat("-other_category")+"").closest('.mydiv').show(); 
    } else { 
     $("#bill-"+index.concat("-other_category")+"").closest('.mydiv').hide(); 
    } 
} 
+0

嗨,感谢您的回复,我之前尝试过..它不起作用。当我有2行,如果我选择“其他”在第二行,它也采取第一行相同。对于其他文本框将显示或否。 –

+0

也试过这个,但没有任何反应,当我选择其他.. –

+0

@SalmanRiyaz。测试和工作,检查控制台是否有任何错误。你的模态名是什么?比尔或其他任何东西。 –