【问题标题】:Yii2: Add new text field on clicking add buttonYii2:单击添加按钮时添加新文本字段
【发布时间】:2015-10-18 12:38:32
【问题描述】:

我想在我的表单中创建文本字段,这样当我点击Add New Field 按钮时,它应该会打开一个新的下一个字段。 这是我的表单代码:

    <?php $form = ActiveForm::begin(); ?>

        <?= $form->field($model, 'status_type')->textInput(['maxlength' => 
true]) ?>

        <div class="form-group">
            <?= Html::submitButton($model->isNewRecord ? 'Create' : 
'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn 
btn-primary']) ?>
        </div>

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

我应该在代码中添加什么来实现这一点。
任何帮助,将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript php jquery yii2


    【解决方案1】:

    我不能清楚地理解你的问题,但是如果你想在你的应用程序中添加动态表单,请尝试先看看这个,如果这个 url 不能解决你的问题,回来我们会讨论如何解决你的问题。 . .

    https://www.youtube.com/watch?v=qqFpTBr323Y

    【讨论】:

      【解决方案2】:

      我找到了解决方法如下:
      Js文件

      jQuery(document).ready(function($){
          $('.my-form .add-items').click(function(){
              var n = $('.text-items').length + 1;
              var box_html = $('<p class="text-items"><label for="item' + n + '">Item <span class="items-number">' + n + '</span></label> <input type="text" class="form-control" name="items[]" value="" id="item' + n + '" /> <a href="#" class="remove-items">Remove</a></p>');
              box_html.hide();
              $('.my-form p.text-items:last').after(box_html);
              box_html.fadeIn('slow');
              return false;
          });
      
          $('.my-form').on('click', '.remove-items', function(){
          //$(this).parent().css( 'background-color', '#FF6C6C' );
          if($('.text-items').length > 1){
              $(this).parent().fadeOut('slow', function() {
                  $(this).remove();
                  $('.items-number').each(function(index){
                      $(this).text( index + 1 );
                  });
              });
          }
          return false;
      });
      });
      

      查看

      <div class="my-form">
      
          <?php
          if( !$model->isNewRecord){
              $items = StatusType::find()->where(['status_id'=>$model->id])->orderBy('order')->all();
              if(!empty($items)){
              foreach ($items as $item) {
          ?>
      
              <p class="text-items">
                  <label for="item<?= $item->order ?>">Item <span class="items-number"><?= $item->order ?></span></label>
                  <input class="form-control" type="text" name="items[]" value="<?= $item->title ?>" id="item<?= $item->order ?>" /><br>
                  <a href="#" class="remove-items">Remove</a>
              </p>
           <?php }}else{
           ?>
              <p class="text-items">
                  <label for="item">Item <span class="items-number">1</span></label>
                  <input class="form-control" type="text" name="items[]" value="" id="item" /><br>
                  <a href="#" class="remove-items">Remove</a>
              </p>
      
           <?php  
              } } else{?>
              <p class="text-items">
                  <label for="item">Item <span class="items-number">1</span></label>
                  <input class="form-control" type="text" name="items[]" value="" id="item" /><br>
                  <a href="#" class="remove-items">Remove</a>
              </p>
           <?php }?>
          <?= Html::button('Add More', ['class'=>'glyphicon glyphicon-plus btn btn-default btn-sm add-items']) ?>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        • 2014-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多