【发布时间】:2010-10-21 05:27:18
【问题描述】:
“this”是一个文本字段,“new_id”是一个整数。
当我应用以下 sn-p 时:
$(this).attr('id', this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).attr('value', 'test');
id 改变了,name 也改变了,但 value 没有改变。如果我将最后一行更改为此(因此使用字符串文字)
$('#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 是否仍然是唯一的?
-
是的,我改了之后就独一无二了
标签: javascript jquery