gpt4 book ai didi

javascript - jQuery:单击 'Add' 按钮后动态附加表格行

转载 作者:行者123 更新时间:2023-11-30 11:03:26 27 4
gpt4 key购买 nike

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

<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--;
})

但是当我按下添加 (+) 按钮时,它没有按预期工作。你能告诉我我该怎么做吗?

谢谢。

最佳答案

不会简单.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>

关于javascript - jQuery:单击 'Add' 按钮后动态附加表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com