“this”是一个文本字段,“new_id”是一个整数。使用jQuery设置新ID
当我申请了下面的代码片段:
$(this).attr('id', this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).attr('value', 'test');
的ID改变,名称也发生了变化,而不是价值。如果我改变最后一行此设置(使用字符串字面)
$('#mytextfield_3').attr('value', 'test');
它的工作原理。
任何想法?
- 编辑 - 感谢Steerpike的快速插件测试 - 我相信它应该可以工作,但我找不到错误。
这里的一些代码:
我创建使用
clone = $(this).find('.clone_fields_container:first').clone();
“克隆”现在包含已嵌入输入字段一个div克隆。
生成一个新的ID后:
/** Iterate over input and select fields in container */
clone.children('input,select,textarea').each(function() {
$(this).attr('id', this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).val('test');
console.log(this);
});
的文本字段不具有任何价值。
是您在更改文档后对于该文档仍然是唯一的ID吗? – fredrik 2009-04-22 21:38:10
是的,我改变它后,它是独一无二的 – schneck 2009-04-22 21:48:12