2017-02-24 82 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。測試和工作,檢查控制檯是否有任何錯誤。你的模態名是什麼?比爾或其他任何東西。 –