当添加新行时,输入名称将增加1
When a new row is added input name goes up by 1?
我正在使用一个按钮向表中添加一行。
当行克隆时,我希望3个文本框上的输入名称增加1。
这就是我现在使用的脚本。(它克隆行并将其添加到下面,但它没有+1输入名称。
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function() {
$('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
$('#mytable tbody>tr:last #product').val('');
$('#mytable tbody>tr:last #qty').val('');
$('#mytable tbody>tr:last #remarks').val('');
$('#mytable tbody>tr:last #add').val('+');
return false;
});
});
这是我的表
<table id="mytable" width="250px" border="0" cellspacing="0" cellpadding="0" style="text-align:center; padding-left:40px; padding-bottom:8px">
<tr class="product">
<td style="text-align:center;"><strong>Item</strong></td>
<td style="text-align:center;"><strong>Qty.</strong></td>
<td style="text-align:center;"><strong>Remarks</strong></td>
</tr>
<tr name="product" class="product">
<td width="100px"><input type="text" width="100px" name="product" id="product" /></td>
<td width="5px" style="text-align:center; padding-left:2px; padding-right:2px;"><input type="text" width="5px" size="1" maxlength="2" name="qty" id="qty" /></td>
<td width="100px"><input type="text" width="100px" name="remarks" id="remarks" /></td>
<td><input type="submit" name="add" id="add" value="+" /></td>
</tr>
</table>
问题是:如何将下一行的输入名称"product"qty"comments"更改为"product1"qty1"remaks1"?而在2行之后的3行之后的行则是无穷多的行。
谢谢你看。
添加到您的click()
处理程序后,以下内容似乎有效:
$('#mytable tbody > tr:last input').each(
function(){
/* the following removes the numerical characters from the id */
var id = this.id.replace(/[0-9]+/g,'');
/* this iterates through each id that starts with the same sequence
of characters */
$('input[id^=' + id + ']').each(
function(i){
/* this sets the id to the sequence of characters,
plus the number of the current iteration of the
each() method */
this.id = id + i;
});
});
JS Fiddle演示。
编辑以回应OP的评论(如下):
但是id增加了1,我需要输入名称增加1。我该如何更改以增加"名称"?
您可以很容易地做到这一点,只需使name
等于内部each()
调用中的id
即可:
$('#mytable tbody > tr:last input').each(
function(){
/* the following removes the numerical characters from the id */
var id = this.id.replace(/[0-9]+/g,'');
/* this iterates through each id that starts with the same sequence
of characters */
$('input[id^=' + id + ']').each(
function(i){
/* this sets the id to the sequence of characters,
plus the number of the current iteration of the
each() method */
this.id = id + i;
this.name = this.id;
});
});
http://jsfiddle.net/davidThomas/WMvRJ/4/
参考文献:
- 属性以选择器
^=
开头 each()
- RegExp,Mozilla开发者网络
replace()
,Mozilla开发者网络
您可以在javascript中设置一个全局变量来跟踪行号,也可以使用javascript解析前一行的行号。我个人会使用全局变量。
相关文章:
- javascript函数,它将数组作为输入,将每个数组元素增加1,并返回增加值的新数组
- 使用jQuery增加输入字段值
- 如何使用jQuery增加type=number输入的值
- 使用JQuery将输入增加1,限制为9,并连接到隐藏输入
- 如何在输入文本字段中添加或增加数值
- 使用 jquery 增加和减少输入的值
- 如何使用 HTML 5 日期输入增加月份 - 将结果输出到 DOM
- 使用 js 或 php 增加表单输入的数量
- 正在使用Javascript来验证“确认密码”输入,增加了任何安全风险
- Javascript增加了输入百分比
- 高位图表增加/减少输入数据
- 当添加新行时,输入名称将增加1
- Jquery插入表行并增加输入id
- 自动增加输入值
- 增加输入标记内的ID属性
- 使用jQuery增加输入字段的值
- 如何在输入字段名称中间增加输入字段名称
- 增加输入类型数字
- 使用鼠标滚轮增加输入值
- 使用Jquery添加行-需要增加输入名称和onkeyup事件调用者