从 onload jquery 添加行到表
我有下表,
<table id="invoice">
<tr>
<th>Quantity</th>
<th>Item</th>
<th><input type="button" id="addnew" name="addnew" value="Add Row" />
</th>
</tr>
<tr id="id1">
<td><input id="quantity1" name="quantity[]" tabindex="1" type="text" value="" /></td>
<td><select id="rate1" name="rate[]" tabindex="2" value="">
<option value="">SELECT</option>
<option value="1">ONE</option>
<option value="2">TWO</option>
</select></td>
<td><input id="remove1" name="remove[]" tabindex="3" type="button" value="Remove Row"class="remove"/></td>
</tr>
</table>
我可以使用以下脚本添加行
var next = 2;
function addrow(table,add)
{
$(add).bind('click', function() {
var $last = $(table + ' tr:last');
var last_row = $last.clone();
$(last_row).find(":input").each(function() {
var store = $(this).attr("id");
var new1 = store.replace(/1/, next);
$(this).attr("id",new1);
});
last_row.appendTo($(table))
$(table+" tr:last").hide().fadeIn('slow');
next++;
});
}
addrow('#invoice','#addnew');
以上脚本克隆第二行并附加到实际需要的表中。但问题是,如果用户在文本框的第二行中输入一些值,然后单击 addrow,它会在新行中复制这些值。为了克服这个问题,我只需更改脚本。
所以我将脚本更改为以下
var next = 2;
function addrow(table,add)
{
var $last = $(table + ' tr:last');///changed this line
var last_row = $last.clone(); /// changed this line
$(add).bind('click', function() {
$(last_row).find(":input").each(function() {
var store = $(this).attr("id");
var new1 = store.replace(/1/, next);
$(this).attr("id",new1);
});
last_row.appendTo($(table))
$(table+" tr:last").hide().fadeIn('slow');
next++;
});
}
这仅添加一行,它只是更改最后一行,但添加的内容不得超过新行。
任何人都可以更正第二个脚本,因为这非常适合我的项目,因为许多其他脚本都与此相关。
I have the following table
<table id="invoice">
<tr>
<th>Quantity</th>
<th>Item</th>
<th><input type="button" id="addnew" name="addnew" value="Add Row" />
</th>
</tr>
<tr id="id1">
<td><input id="quantity1" name="quantity[]" tabindex="1" type="text" value="" /></td>
<td><select id="rate1" name="rate[]" tabindex="2" value="">
<option value="">SELECT</option>
<option value="1">ONE</option>
<option value="2">TWO</option>
</select></td>
<td><input id="remove1" name="remove[]" tabindex="3" type="button" value="Remove Row"class="remove"/></td>
</tr>
</table>
I am able to add row using the following script
var next = 2;
function addrow(table,add)
{
$(add).bind('click', function() {
var $last = $(table + ' tr:last');
var last_row = $last.clone();
$(last_row).find(":input").each(function() {
var store = $(this).attr("id");
var new1 = store.replace(/1/, next);
$(this).attr("id",new1);
});
last_row.appendTo($(table))
$(table+" tr:last").hide().fadeIn('slow');
next++;
});
}
addrow('#invoice','#addnew');
the above script clones the 2nd row and append to the table which is actually required. But the problem is if user inputs some value in the 2nd row of the text box and after that clicks addrow, it replicates this in the new row with the values.To overcome this i just chnges the script.
SO i changed the script to the following
var next = 2;
function addrow(table,add)
{
var $last = $(table + ' tr:last');///changed this line
var last_row = $last.clone(); /// changed this line
$(add).bind('click', function() {
$(last_row).find(":input").each(function() {
var store = $(this).attr("id");
var new1 = store.replace(/1/, next);
$(this).attr("id",new1);
});
last_row.appendTo($(table))
$(table+" tr:last").hide().fadeIn('slow');
next++;
});
}
This adds only one row,it just changes the last row,but not add more than new row.
Can any one please correct this second script as this works perfect for my project as many other scripts are associated with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望每次单击时都创建一个新的克隆/DOM 行。否则你就继续移动一排。
因此,我们在开始时要克隆原始的最后一行,并继续克隆“干净的行”。干净的行保持干净,因为
last_row
从未添加到 DOM。@patrickdw 提到了另一个选项,即继续克隆单击函数内的最后一行,但清除该行的数据。根据您的行的情况,每次清理它可能是值得的。
@patrickdw 方法的好处是您可以清理一些数据但保留其他数据。这可能对你有用。
You want to create a new clone / DOM row each time you click it. Otherwise you just keep moving one row around.
So we have our original last row to clone at the start and we keep cloning the "clean row". The clean row stays clean because
last_row
is never added to the DOM.@patrickdw mentioned another option which is to keep cloning the last row inside the click function but clearing the data of the row. Depending on what your row is like cleaning it up each time might be worthwhile.
The nice thing about @patrickdw method is that you can clean up some data but keep other data. This might be useful for you.