使用jQuery设置新的id
- 论坛
- 使用jQuery设置新的id
20 浏览
使用jQuery设置新的id
"this"是一个文本字段,"new_id"是一个整数。
当我应用以下代码段时:
$(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();
"clone"现在包含一个嵌入了输入字段的div。
在生成新的id之后:
/** 迭代容器中的输入和选择字段 */ 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); });
文本字段没有任何值。