wbraganca dynamicform按钮改变文本字段值

时间:2017-12-18 13:55:53

标签: javascript yii2 dynamicform

按钮setDefault中的textfield中的更改值存在问题。

点击此按钮后,我无法访问并更改“Martin”按钮上方名称文本字段中的值。

我为此设置了id“idN2”,但没有任何事情发生。

请帮助我。

查看:

<?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' => 4, // the maximum times, an element can be cloned (default 999)

    'min' => 0, // 0 or 1 (default 1)

    'insertButton' => '.add-item', // css class

    'deleteButton' => '.remove-item', // css class

    'model' => $modelsAddress[0],

    'formId' => 'dynamic-form',

    'formFields' => [

        'name',

    ],

]); ?>

<div class="panel panel-default">

    <div class="panel-heading">

        <i class="fa fa-envelope"></i> Test

        <button type="button" class="pull-right add-item btn btn-success btn-xs"><i class="fa fa-plus"></i> Add name</button>



        <div class="clearfix"></div>

    </div>

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

        <?php foreach ($modelsAddress as $index => $modelAddress): ?>

            <div class="item panel panel-default"><!-- widgetBody -->

                <div class="panel-heading">

                    <span class="panel-title-address">Names: <?= ($index + 1) ?></span>

                    <button type="button" class="pull-right remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>

                    <div class="clearfix"></div>

                </div>

                <div class="panel-body">

                    <?php
                   $testr=5;
                        // necessary for update action.

                        if (!$modelAddress->isNewRecord) {

                            echo Html::activeHiddenInput($modelAddress, "[{$index}]id");

                        }

                    ?>


                    <?= $form->field($modelAddress, "[{$index}]name")->textInput(['maxlength' => true,'id'=>"idN2"]) ?>


                     <button type="button" onclick="setDefaultF($(this))" class="pull-right setDefault btn btn-success btn-xs"><i class="fa fa-plus"></i> SetDefault</button>
                </div>

            </div>

        <?php endforeach; ?>

    </div>

</div>

<?php DynamicFormWidget::end(); ?>
$this->registerJs('
    function setDefaultF(obj){

        $(\'#idN2\').val("Martin");

    }
', \yii\web\View::POS_HEAD) ?>

我也试过JS函数:

$('#modelAddress-0-name').val("Martin");
alert($('#modelAddress-0-name').val());

但在弹出窗口中我只看到未定义

example form

0 个答案:

没有答案