H
H
hollanditkzn2017-05-05 14:07:45
JavaScript
hollanditkzn, 2017-05-05 14:07:45

How to add new inputs in yii2?

Maybe there is, of course, some kind of widget in yii2 that can add new inputs, while I'm doing jquery, but I can't add a line after the second addition, I don't understand what the problem might be. That is, I need to implement adding multiple records to the database
In the form

<?php $form = ActiveForm::begin(); ?>
    <div id="customForm">
    <?= $form->field($custom, 'tovar')->textInput(['maxlength' => true]) ?>

    <?= $form->field($custom, 'number')->textInput(['type' => 'number', 'min' => '0']) ?>

    <?= Html::button('+', ['class' => 'btn btn-success', 'id' => 'addCustom']) ?>
    </div>
    <div class="form-group">
        <?= Html::submitButton($custom->isNewRecord ? 'Создать' : 'Редактировать', ['class' => $custom->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

js file
$(document).ready(function($){
  $('#addCustom').click(function() {
    var html = $(this).parent().html();
    $(this).remove();
    $("#customForm").append('<div>'+html+'</div>');
    return false;
    });
});

But as I said, it turns out to add only once, the second time it doesn’t work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-05-05
@hollanditkzn

Do not reinvent the wheel, there are ready-made widgets for adding inputs, for example here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question