克隆多个输入Javascript

时间:2019-02-12 21:04:17

标签: javascript jquery

有人可以帮我这个JavaScript吗?我正在尝试克隆多个不同的输入。单击附加按钮时,我想独立克隆付款/付款。众议院负责我要克隆的选择和输入。我能够以我想要的方式克隆标准输入,但也希望使用它来克隆选择框。我也似乎可以使附加的按钮正确排列。我在JavaScript方面很糟糕,因此甚至不确定我是否会以正确的方式进行操作。任何帮助表示赞赏。

$(function() {
  $(document).on('click', '.btn-add', function(e) {
    e.preventDefault();

    var controlForm = $('.controls stuff:first'),
      currentEntry = $(this).parents('.entry:first'),
      newEntry = $(currentEntry.clone()).appendTo(controlForm);

    newEntry.find('input').val('');
    controlForm.find('.entry:not(:last) .btn-add')
      .removeClass('btn-add').addClass('btn-remove')
      .removeClass('btn-success').addClass('btn-danger')
      .html('<span class="glyphicon glyphicon-minus"></span>');
  }).on('click', '.btn-remove', function(e) {
    $(this).parents('.entry:first').remove();

    e.preventDefault();
    return false;
  });
});
.entry:not(:first-of-type) {
  margin-top: 10px;
}

.glyphicon {
  font-size: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="col-md-8 col-md-offset-2">
    <legend>Payouts/Payins - Richfield</legend>

    <!-- Pay Out input-->
    <div class="form-group line">
      <label class="col-md-3 control-label" for="pay_out">Payment Out</label>
      <div class="col-md-4">
        <div class="entry input-group">
          <input id="pay_out" name="pay_out[]" class="form-control" placeholder="Total $" type="text">
          <span class="input-group-btn">&lt;
        <button class="btn btn-success btn-add" type="button">
            <span class="glyphicon glyphicon-plus"></span>
          </button>
          </span>
        </div>
      </div>
    </div>

    <!-- press + div -->
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <small>
            Press
             <span class="glyphicon glyphicon-plus gs"></span>
               to add another form field :)
             </small>
      </div>
    </div>
    <br>

    <!-- Pay In input-->
    <div class="form-group line">
      <label class="col-md-3 control-label" for="pay_in">Payment In</label>
      <div class="col-md-4">
        <div class="entry input-group">
          <input id="pay_in" name="pay_in[]" class="form-control" placeholder="Total $" type="text">
          <span class="input-group-btn">&lt;
        <button class="btn btn-success btn-add" type="button">
            <span class="glyphicon glyphicon-plus"></span>
          </button>
          </span>
        </div>
      </div>
    </div>

    <!-- press + div -->
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <small>
            Press
             <span class="glyphicon glyphicon-plus gs"></span>
               to add another form field :)
             </small>
      </div>
    </div>


    <br>
    <legend>House Charges - Richfield</legend>


    <!-- Dynamic House charges Divider-->
    <div class="col-md-3 col-md-offset-2 colspace">
      <div class="form-group">
        <select class="form-control" id="account_selector" name="house_account[]">
          <option>Select House Acct</option>
          <option>2</option>
          <option>3</option>
          <option>4</option>
          <option>5</option>
        </select>
      </div>

    </div>
    <div class="col-md-3 colspace">
      <div class="form-group">
        <div class="controls">
          <stuff>
            <div class="entry input-group">
              <input id="house_total" name="house_total[]" class="form-control" placeholder="Total $" type="text">
              <span class="input-group-btn">
                          <button class="btn btn-success btn-add" type="button">
                              <span class="glyphicon glyphicon-plus"></span>
              </button>
              </span>
            </div>

          </stuff>
        </div>
      </div>

    </div>
  </div>
</div>


<div id="push"></div>

Bootply

1 个答案:

答案 0 :(得分:0)

我明白了,如果可以帮助其他人,这是我的代码。

$(function(){
        $(document).on('click', '.btn-add', function(e) {
            e.preventDefault();
            var parentRow = $(this).closest('.charge-row'),
                cloneRow = cloneChargeRow(parentRow);

            $('.charge-row:last').after(cloneRow);

        }).on('click', '.btn-remove', function(e) {
            $(this).closest('.charge-row').remove();

            e.preventDefault();
            return false;
        });

        function cloneChargeRow(parentRow) {
          // Clear existing add button
          $('.charge-row').find('.btn-add').removeClass('btn-add btn-success').addClass('btn-remove btn-danger');
          $('.charge-row').find('.btn').html('<span class="glyphicon glyphicon-minus"></span>')


          var newRow = parentRow.clone();

          newRow.find('.btn').addClass('btn-add btn-success').removeClass('btn-remove btn-danger');
          newRow.find('.btn').html('<span class="glyphicon glyphicon-plus"></span>');
          newRow.find('select :selected').removeAttr('selected');
          newRow.find('input').val('');

          return newRow;
        }
    });

这是html:

        <div class="container">
      <div class="col-md-10">

    <legend>House Charges - Richfield</legend>
        <div class="charge-row row">
          <!-- Dynamic House charges Divider-->
          <div class="col-md-3 col-md-offset-2 colspace">
            <div class="form-group">
              <select class="form-control" id="account_selector" name="house_account[]">
                <option>Select House Acct</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
              </select>
            </div>                        
          </div>

          <div class="col-md-3 colspace">
            <div class="form-group">
              <div class="controls">
                <div class="entry input-group">
                  <input id="house_total" name="house_total[]" class="form-control" placeholder="Total $" type="text">
                  <span class="input-group-btn">
                    <button class="btn btn-success btn-add" type="button">
                      <span class="glyphicon glyphicon-plus"></span>
                    </button>
                  </span>
                </div>
              </div>
            </div>            
          </div>
        </div>
      </div>
    </div>

Bootply Here