我需要10行而不是1行

时间:2018-04-24 11:52:18

标签: javascript jquery

我制作了这段代码,每次点击按钮都会添加一个新行,但我想从10行而不是1行开始。有没有人知道我该怎么做?

$(document).ready(function() {
  var max_fields = 15; //maximum input boxes allowed
  var wrapper = $(".input_fields_wrap"); //Fields wrapper
  var add_button = $(".add_field_button"); //Add button ID

  var x = 1; //initlal text box count
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    if (x < max_fields) { //max input box allowed
      x++; //text box increment
      $(wrapper).append('<div><a href="#" class="remove_field">Regel verwijderen</a><input type="text" name="mytext[]" class="text"/></div>'); //add input box
    }
  });

  $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
    e.preventDefault();
    $(this).parent('div').remove();
    x--;
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="width">
  <form>
    <div class="input_fields_wrap">
      <input type="button" name="plus" value="Regel toevoegen" class="add_field_button btn">
      <div><input type="text" name="mytext[]" class="text"></div>
    </div>
    <input type="submit" name="submit" value="Done" class="submit btn">
</div>
</form>

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
$(document).ready(function () {
  var defrow = 10 // default rows
  var max_fields = 15 // maximum input boxes allowed
  var wrapper = $('.input_fields_wrap') // Fields wrapper
  var add_button = $('.add_field_button') // Add button ID

  var x = 1 // initlal text box count
  Array(defrow)
    .fill()
    .forEach(function () {
      $(wrapper).append(
        '<div><a href="#" class="remove_field">Regel verwijderen</a><input type="text" name="mytext[]" class="text"/></div>'
      ) // add input box
    })
  $(add_button).click(function (e) {
    // on add input button click
    e.preventDefault()
    if (x < max_fields) {
      // max input box allowed
      x++ // text box increment
      $(wrapper).append(
        '<div><a href="#" class="remove_field">Regel verwijderen</a><input type="text" name="mytext[]" class="text"/></div>'
      ) // add input box
    }
  })

  $(wrapper).on('click', '.remove_field', function (e) {
    // user click on remove text
    e.preventDefault()
    $(this)
      .parent('div')
      .remove()
    x--
  })
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="add_field_button">button</button>
<div class="input_fields_wrap"></div>
&#13;
&#13;
&#13;

我添加此代码:

  Array(defrow)
    .fill()
    .forEach(function () {
      $(wrapper).append(
        '<div><a href="#" class="remove_field">Regel verwijderen</a><input type="text" name="mytext[]" class="text"/></div>'
      ) // add input box
    })