克隆选择以及其他输入标记
Clone select along with other input tags?
我正在尝试克隆一个表行并生成一个id为的数组,因为用户可以插入n行。我面临的问题是,我在该表行中有1个选择下拉选项。如何将一个选择与一行中的其他输入标记一起克隆?(这段代码一次生成两组行,因为有两个appendTo()。谢谢你的帮助!
$("#add").click(function() {
$("#comTable tr:eq(0)").clone().find("input").each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
}).end().appendTo("#comTable");
$("#comTable tr:eq(0)").clone().find("select").each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
}).end().appendTo("#comTable");
i++;
});
您只需选择input
和select
元素,如下所示:
旧代码:
$("#comTable tr:eq(0)").clone().find("input").each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
}).end().appendTo("#comTable");
$("#comTable tr:eq(0)").clone().find("select").each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
}).end().appendTo("#comTable");
新代码:
$("#comTable tr:eq(0)").clone().find("input, select").each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
}).end().appendTo("#comTable");
注意"input, select"
选择器。
编辑
如果你想以不同的方式处理select
,你可以用另一种方式:
$("#comTable tr:eq(0)")
.clone()
.find("input")
.each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
})
.end() //End .find("input")
.find("select")
.each(function() {
// creates array of ids if the user wants to add more than 1 row so it is N[1], etc
$(this).val('').attr('id', function(_, id) {
return id + 'N' + '[' + i + ']'
});
$(this).val('').attr('name', function(_, name) {
return name + 'N' + '[' + i + ']'
});
})
.end() //End .find("select")
.appendTo("#comTable");
i++;
这种方式删除了额外的克隆,并在新克隆的DOM元素上再次运行.find
。
相关文章:
- JQuery:当其他输入字段的值发生更改时更改值
- 输入类型=文件验证停止其他输入类型验证
- 如何根据其他日期的输入更新日期
- 用户名输入如果其他块不能正常工作/Javascript-jQuery-AJAX
- 单击其他元素或鼠标向上时隐藏输入框,但保留一次焦点
- 单击列表中的引导输入按钮将禁用其他按钮
- 在维护输入内容的同时添加其他字段
- 如果所有其他元素都填写在AngularJS中,如何动态添加新的输入元素
- 在其他文本输入字段中输入文本时启用复选框
- 使用其他输入值自动填充输入
- 创建一个计算值数组,该数组绑定到AngularJS中的其他输入
- 更新其中一个字段时刷新其他输入字段.使用Angularjs(附Plunker)
- 停止jQuery表单验证重置由其他JavaScript填充的typeahead.js输入字段
- 根据选择选项启用/禁用其他输入
- 如果我用javascript隐藏了一个文本输入元素,那么其他javascript函数仍然可以访问输入到其中的值
- 选择列表框以更改其他输入
- Matrix3d 属性值及其他
- 如何阻止恶意自动iFrame表单输入从其他网站
- 输入文本框应该包含$和£货币符号和警告,如果用户输入任何其他特殊字符
- 如何确保如果值在一个字段中输入,其他(伴随)字段/下拉列表必须填写/选择