jQuery:单击“添加”按钮后动态追加表格行

时间:2019-06-19 07:04:45

标签: javascript jquery html dom

我有这个html表单,其中为字段添加了一个按钮。

<form>
    <table width="50%" align="center">
        <tr>
            <td>Amount</td>
            <td><input type="number" name="amount" id="amount" required></td>
        </tr>
        <tr>
            <td>Buyer</td>
            <td><input type="text" name="buyer" id="buyer" maxlength="255" required></td>
        </tr>
        <tr>
            <td>Receipt Id</td>
            <td><input type="text" name="receipt_id" name="receipt_id" maxlength="20" required></td>
        </tr>       
        <tr class="input_fields_wrap">
            <td>Items</td>
            <td><input type="text" name="items[]" class="items" required>&nbsp;<button class="add_field_button">Add+</button></td>
        </tr>               
        <tr>
            <td>Buyer Email</td>
            <td><input type="email" name="buyer_email" id="buyer_email" required></td>
        </tr>
        <tr>
            <td>Note</td>
            <td><input type="text" name="note" id="note" maxlength="30" required></td>
        </tr>
        <tr>
            <td>City</td>
            <td><input type="text" name="city" id="city" maxlength="20" required></td>
        </tr>
        <tr>
            <td>Phone</td>
            <td><input type="text" name="phone" id="phone" required></td>
        </tr>
        <tr>
            <td>Entry By</td>
            <td><input type="text" name="entry_by" id="entry_by" required></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="Recrod Sales"></td>
        </tr>
    </table>
</form>

现在,我想在 item 字段

之后添加 items字段的另一个副本

为此,我正在使用此jQuery:

var max_fields      = 10; //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
        var form = '<div><tr><td>Items</td><td><input type="text" name="items[]" class="items" required>&nbsp;<button class="add_field_button">Add+</button></td></tr><a href="#" class="remove_field">Remove</a></div>';
        //$(wrapper).append(form); //add input box
        $(form).after(wrapper);

    }
});

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
    e.preventDefault(); $(this).parent('div').remove(); x--;
})

但是当我按下那个add(+)按钮时,它无法正常工作。你能告诉我该怎么做吗?

谢谢。

3 个答案:

答案 0 :(得分:3)

.insertAfter()对您有用吗? :

const itemHtml = `<tr class="input_fields_wrap"><td>Items</td><td><input type="text" name="items[]" class="items" required>&nbsp;<button class="add_field_button">Add+</button></td></tr>`;

$('form').on('click', '.add_field_button', function(event){
  event.preventDefault();
  $(itemHtml).insertAfter($(event.target).closest('tr'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><form><table width="50%" align="center"><tr><td>Amount</td><td><input type="number" name="amount" id="amount" required></td></tr><tr><td>Buyer</td><td><input type="text" name="buyer" id="buyer" maxlength="255" required></td></tr><tr><td>Receipt Id</td><td><input type="text" name="receipt_id" name="receipt_id" maxlength="20" required></td></tr>       <tr class="input_fields_wrap"><td>Items</td><td><input type="text" name="items[]" class="items" required>&nbsp;<button class="add_field_button">Add+</button></td></tr>               <tr><td>Buyer Email</td><td><input type="email" name="buyer_email" id="buyer_email" required></td></tr><tr><td>Note</td><td><input type="text" name="note" id="note" maxlength="30" required></td></tr><tr><td>City</td><td><input type="text" name="city" id="city" maxlength="20" required></td></tr><tr><td>Phone</td><td><input type="text" name="phone" id="phone" required></td></tr><tr><td>Entry By</td><td><input type="text" name="entry_by" id="entry_by" required></td></tr><tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Recrod Sales"></td></tr></table></form>

答案 1 :(得分:0)

代码中的$(add_button)必须为add_button,因为add_button已经是一个jQuery对象。

NOTE ::同样按照惯例,请保存一个以$开头的变量名称的jQuery对象

要添加元素,可以使用clone()方法,因此jQuery avaoid删除当前元素。像这样::-

var max_fields      = 10; //maximum input boxes allowed
var wrapper         = $(".input_fields_wrap").clone(); //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
          var form = $(`
            <div>
              <tr class="input_fields_wrap"><td>Buyer Email</td><td>
                <input type="email" name="buyer_email" id="buyer_email" required>
              </td></tr>
              <a href="#" class="remove_field">Remove</a>
            </div>';
          `)
          //$(wrapper).append(form); //add input box
          $('form').after(wrapper);

      }
  });

对于除去部分,没有类.remove_field的元素,因此请首先研究。

答案 2 :(得分:0)

尝试

var itemHtml='<tr class="input_fields_wrap">
        <td>Items</td>
        <td><input type="text" name="items[]" class="items" required></td>
        </tr>';

$('.add_field_button').click(function(){
   $(itemHtml).insertAfter($(this).closest('tr'));
});