dongxian8858 2016-11-01 11:52
浏览 166
已采纳

在动态形式yii2中添加新行,在新行中,js不起作用

I have a case to manage a dynamic input in yii2. So I choose this extension : wbraganca

This is my code :

<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
    <div class="col-md-8">
        <div class="padding-v-md">
            <div class="line line-dashed"></div>
        </div>

        <?php
        DynamicFormWidget::begin([

            'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
            'widgetBody' => '.container-items', // required: css class selector
            'widgetItem' => '.item', // required: css class
            'limit' => 10, // the maximum times, an element can be cloned (default 999)
            'min' => 1, // 0 or 1 (default 1)
            'insertButton' => '.add-item', // css class
            'deleteButton' => '.remove-item', // css class
            'model' => $modelsTransactionDetail[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'item_id',
                'karyawan_id',
                'harga_jual',
                'jumlah_jual',
                'subtotal'
            ],
        ]);
        ?>

        <div class="panel panel-default">
            <div class="panel-heading">
                <i class="fa fa-money"></i> Item Pembelian
                <div class="clearfix"></div>
            </div>

            <div class="panel-body"><!-- widgetContainer -->

                <table class="table table-bordered table-striped container-items">
                    <thead>
                        <tr>
                            <th style="width: 30%">Item</th>
                            <th style="width: 15%">By</th>
                            <th style="width: 18%">Harga</th>
                            <th style="width: 8%">Qty</th>
                            <th style="width: 25%">Sub Total</th>
                            <th class="text-center" style="width: 5%">Act</th>
                        </tr>

                    </thead>
                    <tbody>
                        <?php foreach ($modelsTransactionDetail as $indexTools => $modelTools): ?>
                            <tr class="item">
                                <td class="vcenter">

                                    <?=
                                    $form->field($modelTools, "[{$indexTools}]item_id")->label(false)->dropDownList(
                                            ArrayHelper::map(ItemLayanan::find()->all(), 'id', 'nama_item'), ['prompt' => 'Select item', 'class' => 'item_id']
                                    )
                                    ?>
                                </td>
                                <td class="vcenter">

                                    <?=
                                    $form->field($modelTools, "[{$indexTools}]karyawan_id")->label(false)->dropDownList(
                                            ArrayHelper::map(Karyawan::find()->all(), 'id', 'nama_karyawan'), ['prompt' => 'Select Karyawan']
                                    )
                                    ?>
                                </td>
                                <td class="vcenter">
                                    <?= $form->field($modelTools, "[{$indexTools}]harga_jual")->label(false)->textInput(['maxlength' => true]) ?>
                                </td>
                                <td class="vcenter">
                                    <?= $form->field($modelTools, "[{$indexTools}]jumlah_jual")->label(false)->textInput(['maxlength' => true]) ?>
                                </td>
                                <td class="vcenter">
                                    <?= $form->field($modelTools, "[{$indexTools}]subtotal")->label(false)->textInput() ?>
                                </td>
                                <td class="text-center vcenter" style="width: 90px;">
                                    <button type="button" class="remove-item btn btn-danger btn-xs"><span class="glyphicon glyphicon-minus-sign"></span></button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="5"></td>
                            <td><button type="button" class="add-item btn btn-success btn-sm"><span class="fa fa-plus"></span> New</button></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
        <?php DynamicFormWidget::end(); ?>
    </div>

Please see in this pieces of code above :

 <?=
    $form->field($modelTools, "[{$indexTools}]item_id")->label(false)->dropDownList(
          ArrayHelper::map(ItemLayanan::find()->all(), 'id', 'nama_item'), ['prompt' => 'Select item', 'class' => 'item_id']
        )
  ?>

The question is, I have a register js, but why this js have work only just once, which is the first row, when I adding a row using wbraganca, it not works in new row:

This is my js

<?php
 $js = <<<'EOD'

     $(".item_id").on('change',function () {
         alert();
     });
 EOD;
 $this->registerJs($js);
 ?>
  • 写回答

1条回答 默认 最新

  • doujiu7704 2016-11-01 12:00
    关注

    Newly added DOM element is not bound and this is why it's not working. Change JS to

    $("body").on("change", ".item_id", function () {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历