处理重复表单字段集 (regex) 中数组 ID 的递增数
handle incrementing number of array ID in duplicated form field set (regex)
我需要复制表单的行(在表格中)
在这里查看jsbin:http://jsbin.com/ExiRAMa/1/edit
标记 :
<div id="o99_the_work">
<table><tbody>
<tr>
<!-- THE ORDER -->
<td>X</td>
<td class="small-text"><span class="wpcf7-form-control-wrap submitted-file"><input type="file" name="submitted-file" value="1" size="40" class="wpcf7-form-control wpcf7-file" id="submitted-file-1"></span></td>
<td><span class="wpcf7-form-control-wrap number-of-copies"><input type="text" name="number-of-copies" value="" size="40" class="wpcf7-form-control wpcf7-text small-text" id="number-of-copies-1"></span></td>
<td><span class="wpcf7-form-control-wrap checkbox-copy-type"><span class="wpcf7-form-control wpcf7-checkbox" id="copy-type-1"><span class="wpcf7-list-item"><input type="checkbox" name="checkbox-copy-type[]" value="color"> <span class="wpcf7-list-item-label">color</span></span><span class="wpcf7-list-item"><input type="checkbox" name="checkbox-copy-type[]" value="bw"> <span class="wpcf7-list-item-label">bw</span></span><span class="wpcf7-list-item"><input type="checkbox" name="checkbox-copy-type[]" value="transperant"> <span class="wpcf7-list-item-label">transperant</span></span><span class="wpcf7-list-item"><input type="checkbox" name="checkbox-copy-type[]" value="pergament"> <span class="wpcf7-list-item-label">pergament</span></span></span></span></td>
<td><span class="wpcf7-form-control-wrap submitted-remarks"><input type="text" name="submitted-remarks" value="" size="40" class="wpcf7-form-control wpcf7-text" id="submitted-remarks-1"></span> </td>
</tr></tbody></table>
<button id="add_row">Add new</button>
</div>
JS:
jQuery("#add_row").click(function() {
var row = jQuery("#k99_the_work tbody > tr:last"),
newRow = row.clone(true);
newRow.find("input[type=text],input[type=file]").each(function() {
var num = +(this.id.match(/'d+$/) || [0])[0] + 1;
this.id = this.id.replace(/'d+$/, "") + num;
this.name = this.id;
});
newRow.insertAfter(row);
return false;
});
正如您从垃圾箱中看到的那样,脚本在input=text上工作正常,并且它正在增加名称和ID - 但我的问题是如何处理复选框。我需要递增名称、ID 等,同时将其checkbox-copy-type[]
单独的数组。意思是,复制后我需要checkbox-copy-type-1[]
,checkbox-copy-type-2[]
等
我绝不是一个正则表达式的人,但我尝试添加:
newRow.find("input[type=checkbox]").each(function() {
var num = +(this.id.match(/checkbox-copy-type/) || [0])[0] + 1;
// this.id = this.name.replace(/'[]/, "vv") + num;
this.id = this.name.replace(/'[]/, "") + num;// take off the brackets
this.id = this.name + "[]" ;// add the brackets again
this.name = this.id;
});
但是当我尝试这样做时,我得到的只是另一组括号,例如 checkbox-copy-type[][]
, checkbox-copy-type[][][]
您可以将项目存储在 html 的数据部分(如果它对于每个 tr/复选框都是唯一的),以这种方式检索它,然后递增它,然后添加括号。
.HTML
<tr data-name="myName"></tr>
JavaScript
newRow.find("input[type=checkbox]").each(function(x, item) {
var name = $(item).data('name'); //this give you the name if it unique
var newName = name + x + '[]' // this give final result
});
我已经这样解决了它:
newRow.find("input[type=checkbox]").each(function() {
var parentid = jQuery(this).parent().parent();
var num = +(this.id.match(/checkbox-copy-type+$/) || [0])[0] + 1;
this.id = this.name.replace(/'d+/, function(val) { return parseInt(val)+1; });
this.name = this.id;
parentid.attr("id",parentid.attr("id").replace(/'d+/, function(val) { return parseInt(val)+1; }));
});
相关文章:
- 保存id数组's而不是对象
- 将对象数组与id数组进行比较
- 使用Javascript向ID数组发送通知时出现Facebook访问令牌错误
- 如何将id数组与带下划线的对象数组嵌套属性进行比较
- 获取jQuery UI排序后的ID数组
- 在主干网中过滤的 ID 数组上使用 .set
- 如何使用带有复选框的事件触发器访问 ID 数组
- 带有谷歌地球 API 的地标 ID 数组
- 使用 jQuery 使用 id 数组选择元素
- 从另一个表中获取 ID 数组的值
- 将应用请求对话框发送到特定的 id 数组
- 我正在尝试遍历 id 数组,如果不存在 id,则插入一个 mongodb 文档.我怎样才能让他们坚持下去
- 来自不同集合的子文档 ID 数组的猫鼬架构
- 如何将 ID 数组与包含其 ID 作为属性名称一部分的对象属性进行匹配
- 当使用sequelize.js给一个id数组时进行多次删除
- 从id数组到名称数组(mongo,nodejs)
- 循环遍历DIV id数组,并每隔x秒刷新这些DIVS
- 获取javascript中id数组相似的所有元素
- 在MongoDB中输入用户名数组,并返回相同大小的id数组,对于不存在的用户名为null或false
- 通过对象列表将 ID 数组映射到字符串数组