2017-02-24 6 views
0

yii2動的フォームwbragancaを使用して動的行を作成しています。これは問題なく機能しています。 私はonchangeがドロップダウンの値に基づいてaをロードするフォームにカテゴリドロップダウンを持っています。ドロップダウンの値が他の場合次にこのdivを読み込んでいます。このdivには、ユーザーが他のカテゴリに入ることができるテキストボックスがあります。Yii2動的フォームwbraganca onchange wwrking

新しい行をクリックすると、最初の行でこの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行目を持っている場合、2行目で「others」を選択すると、1行目も同じようになります。両方の他のテキストボックスが表示されるか、そうでない場合は表示されません。 –

+0

でもこれを試しましたが、他のものを選ぶと何も起こりません。 –

+0

@SalmanRiyaz。テストして動作していれば、エラーがないかコンソールで確認してください。あなたのモーダルネームは何ですか?ビルまたは何か他のもの。 –

関連する問題