[英]Add and remove div rows to fieldset dynamically , JQuery
I have a fieldset below 我在下面有一个字段
<fieldset>
<div class="panel panel-default">
<div class="panel-heading clearfix">Collection photos</div>
<div class="panel-body shopping-cart">
<div class="row product-list title hidden-xs">
<div class="col-xs-4 col-sm-4">Product</div>
<div class="col-xs-6 col-sm-6 title">Description</div>
<div class="col-xs-1 center col-sm-1">Action</div>
</div>
<div class="row product-list">
<div class="col-xs-4 col-sm-4">
<a href="product.php">
<img src="images/products/1.jpg" class="img-responsive" alt="product">
</a>
</div>
<div class="col-xs-6 col-sm-6 title">
<textarea id="txtDescription" rows="2">Photo Description</textarea>
</div>
<div class="col-xs-1 col-sm-1 center">
<label>Open
<i class="fa fa-folder-open-o fa-2x">
<input type="file" style="display: none">
</i>
</label>
</div>
</div>
<div class="row product-list grandtotal">
<div class="col-xs-4 col-sm-4">Total</div>
<div class="col-xs-6 col-sm-6 title">
<label for="lblPhotoTotal">0</label>
</div>
<div class="col-xs-1 center col-sm-1">
<a href="#" class="btn btn-primary" id="btnAdd">Add</a>
</div>
</div>
</div>
</div>
</fieldset>
I have three rows at the moment , THe first contain the fieldset header, the second is the default first field. 我现在有三行,第一行包含fieldset标头,第二行是默认的第一字段。 The third contains by buttons (ie add button).
第三个包含按按钮(即添加按钮)。 I want to add another row similar to the second row when ever a user click the add button .
我想在用户单击添加按钮时添加与第二行相似的另一行。 I want the row to be right before the last row containing my add button .
我希望该行位于包含我的添加按钮的最后一行之前。 When i try the Jquery below , it seems to be selecting my fieldset to right to the panel-body and shopping-cart classes.
当我尝试下面的Jquery时,似乎正在将我的字段集选择在panel-body和shopping-cart类的右侧。
$('fieldset').find('.panel-body ,.shopping-cart');
However , my new row is not appending. 但是,我的新行没有追加。 Please how to i append the new instance of the second row right before the last row each time the button is click ?
请如何在每次单击按钮时在最后一行之前添加第二行的新实例? ie
即
<div class="row product-list">
<div class="col-xs-4 col-sm-4">
<a href="product.php">
<img src="images/products/1.jpg" class="img-responsive" alt="product">
</a>
</div>
<div class="col-xs-6 col-sm-6 title">
<textarea id="txtDescription" rows="2">Photo Description</textarea>
</div>
<div class="col-xs-1 col-sm-1 center">
<label>Open
<i class="fa fa-folder-open-o fa-2x">
<input type="file" style="display: none">
</i>
</label>
</div>
</div>
This is how I would do it: 这就是我要做的:
$("#btnAdd").click(function() {
$($(".shopping-cart").children().get(1)).clone().insertBefore($(".shopping-cart").children().last());
});
I made a little example in jsFiddle. 我在jsFiddle中做了一个小例子 。 It is just to show you how it could work.
只是为了向您展示它如何工作。
HTML HTML
<table>
<thead>
<tr>
<th>Date</th>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr>
<td>05/01/2016</td>
<td>John Doe</td>
<td>Male</td>
</tr>
<tr>
<td>05/01/2016</td>
<td>Jane Doe</td>
<td>Female</td>
</tr>
<tr>
<td colspan="3" class="button">
<button id="newRow">
add row
</button>
</td>
</tr>
</tbody>
</table>
Some CSS 一些CSS
table {
border: 2px solid black;
}
th {
border-bottom: 2px solid black;
}
.button {
text-align: center;
}
button {
width: 100%;
}
jQuery jQuery的
$("#newRow").click(function() {
$(this).parents("tr").before($(this).parents("tr").prev().clone());
})
You just have to clone your previous row-element ( <tr>
) and place it before the row where your button is in. .parents("tr")
finds the closest parent that is a <tr>
-element) and .before()
places whatever you want before the element you're using it on (in your case, the row with your button) 您只需克隆先前的row-element(
<tr>
)并将其放在按钮所在的行之前.parents("tr")
找到最接近的父级,该父级是<tr>
.before()
)和.before()
将您想要的任何东西放在要使用它的元素之前(在您的情况下,是带有按钮的行)
UPDATE Optimized my code, read the question wrong. UPDATE优化了我的代码,错误地读了这个问题。 I thought it was just about appending a new row, not cloning the previous one.
我以为这只是追加新行,而不是克隆前一行。
I would choose CSS selectors to copy a blank new row. 我会选择CSS选择器来复制空白新行。
// Get row while its blank
var row = $('.product-list:nth-child(2)').clone();
// Copy blank row everytime the user clicks
$('#btnAdd').on('click', function () {
row.clone().insertBefore('.product-list:last-child')
});
If you want to copy the previous row you could use this 如果要复制上一行,可以使用此行
$('#btnAdd').on('click', function () {
$('.product-list:nth-last-child(2)').clone().insertBefore('.product-list:last-child');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.