"this"是文本字段,"new_id"是整数.
当我应用以下代码段时:
$(this).attr('id', this.id + '_' + new_id);
$(this).attr('name', this.name + '_' + new_id);
$(this).attr('value', 'test');
Run Code Online (Sandbox Code Playgroud)
id更改,名称也会更改,但不会更改.如果我将最后一行更改为此(因此使用字符串文字)
$('#mytextfield_3').attr('value', 'test');
Run Code Online (Sandbox Code Playgroud)
有用.
有任何想法吗?
- 编辑 - 感谢Steerpike的快速插件测试 - 我相信它应该可行,但我找不到错误.
这是一些更多的代码:
我使用创建克隆
clone = $(this).find('.clone_fields_container:first').clone();
Run Code Online (Sandbox Code Playgroud)
"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);
});
Run Code Online (Sandbox Code Playgroud)
文本字段没有任何值.
我有一个包含几个元组的列表,如:
[('a_key', 'a value'), ('another_key', 'another value')]
Run Code Online (Sandbox Code Playgroud)
其中第一个元组值充当字典键.我现在正在寻找一种类似python的方式来访问键/值对,例如:
"mylist.a_key" 要么 "mylist['a_key']"
没有迭代列表.有任何想法吗?