Answer the question
In order to leave comments, you need to log in
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(); ?>
$(document).ready(function($){
$('#addCustom').click(function() {
var html = $(this).parent().html();
$(this).remove();
$("#customForm").append('<div>'+html+'</div>');
return false;
});
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question