[英]How to add more fields using jQuery?
I have a 3 input fields with +Add More Button. 我有一个带有+ Add More按钮的3个输入字段。 I want to add more fields when +Add More button is pressed. 我想在按下“ 添加更多”按钮时添加更多字段。
When New field will create there should be a remove button to remove newly added row and new row can be create by using +Add More button. 当New字段创建时,应该有一个删除按钮来删除新添加的行,并且可以使用+ Add More按钮创建新行。
For that I am using following html and jQuery code but can't get the idea how to add / remove fields ! 为此,我使用以下html和jQuery代码,但无法理解如何添加/删除字段!
html and jQuery Code HTML和jQuery代码
<div id="tab_logic" class="after-add-more">
<div class="col-md-4">
<div class="form-group">
<label class="control-label">Logger Name</label>
<input maxlength="200" type="text" class="form-control" placeholder="Enter Logger Name" name="lg_name[]" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Logger Serial Number</label>
<input maxlength="200" type="text" class="form-control" placeholder="Enter Serial Number" name="lg_sl[]" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Modem Serial Number</label>
<input maxlength="200" type="text" class="form-control" placeholder="Enter Modem Serial Number" name="lg_md_sl[]" />
</div>
</div>
<div class="remove-button"></div>
</div>
<div class="col-md-2">
<div class="form-group change">
<label for=""> </label><br/>
<a class="btn btn-success add-more">+ Add More</a>
</div>
</div>
<div class="more-feilds"></div>
$(document).ready(function() {
$(".add-more").click(function(){
var html = $("#tab_logic").html();
$(".more-feilds").append(html);
});
$("body").on("click",".remove",function(){
$(this).parents("#tab_logic").remove();
});
});
Update : 更新:
I have updated my html and jquery code. 我已经更新了我的html和jquery代码。 Now I have +Add More button. 现在我有+添加更多按钮。 When I pressed the button it's adding 3 new input fields which I want. 当我按下按钮时,它会添加3个我想要的新输入字段。 But I want to add remove button to each newly created 3 fields to remove it. 但我想为每个新创建的3个字段添加删除按钮以将其删除。
How can I do this ? 我怎样才能做到这一点 ?
you can use the below logic. 你可以使用以下逻辑。 this will clone the first div.after-add-more and add the remove button on the html. 这将克隆第一个div.after-add-more并在html上添加删除按钮。
PS : i have removed the id to avoid duplicate ids in html.ID attribute is not required for this functionality, hope that is not an issue with you. PS:我已经删除了id以避免在html中出现重复的ID。此功能不需要该属性,希望这不是你的问题。
$(document).ready(function() { $("body").on("click",".add-more",function(){ var html = $(".after-add-more").first().clone(); // $(html).find(".change").prepend("<label for=''> </label><br/><a class='btn btn-danger remove'>- Remove</a>"); $(html).find(".change").html("<label for=''> </label><br/><a class='btn btn-danger remove'>- Remove</a>"); $(".after-add-more").last().after(html); }); $("body").on("click",".remove",function(){ $(this).parents(".after-add-more").remove(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="after-add-more"> <div class="col-md-4"> <div class="form-group"> <label class="control-label">Logger Name</label> <input maxlength="200" type="text" class="form-control" placeholder="Enter Logger Name" name="lg_name[]" /> </div> </div> <div class="col-md-3"> <div class="form-group"> <label class="control-label">Logger Serial Number</label> <input maxlength="200" type="text" class="form-control" placeholder="Enter Serial Number" name="lg_sl[]" /> </div> </div> <div class="col-md-3"> <div class="form-group"> <label class="control-label">Modem Serial Number</label> <input maxlength="200" type="text" class="form-control" placeholder="Enter Modem Serial Number" name="lg_md_sl[]" /> </div> </div> <div class="col-md-2"> <div class="form-group change"> <label for=""> </label><br/> <a class="btn btn-success add-more">+ Add More</a> </div> </div> </div>
Use .append()
instead of .html()
. 使用.append()
代替.html()
。 The .append function insert content to the end of each element in the set of matched elements and .html will replace the existing elements. .append函数将内容插入到匹配元素集中的每个元素的末尾,而.html将替换现有元素。
$(document).ready(function() {
$(".add-more").click(function(){
var html = $("#tab_logic").html();
$(".after-add-more").after(html);
$(".change").append("<label for=''> </label><br/><a class='btn btn-danger remove'>- Remove</a>");
});
$("body").on("click",".remove",function(){
$(this).parents("#tab_logic").remove();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.