我在执行属性值的查找和替换时遇到困难。
考虑一下这个html:
<tr id="rules[0]">
<td>
<input id="rules0.isActive1" name="rules[0].isActive" type="checkbox" value="true"/><input type="hidden" name="_rules[0].isActive" value="on"/>
</td>
<td>
<select id="rules0.leftCondition.name" name="rules[0].leftCondition.name">
...
</select>
</td>
我希望用适当的计数更新每个人的名称属性,例如,rules.isActive更新为rules10.isActive
我正在使用这个JQuery代码片段:
$(newRow).find('[id*="rules"]').each(function(){
// Update the 'rules[0]' part of the name attribute to contain the latest count
this.name.replace('rules\[0\]','rules\[$count\]');
});
其中newRow是保存整个块的变量,count保存最新的计数器值。但是,当我调试这段代码时,似乎"this.name“是未定义的。
我在哪里做错了?
谢谢
发布于 2012-01-04 23:39:45
要更新元素的属性,应该使用.attr函数。此外,在jQuery上下文中使用它时,必须使用$( this )语法。如果我理解你的意图,那么应该是这样的:
$(newRow).find('[id*="rules"]').each(function(){
// Update the 'rules[0]' part of the name attribute to contain the latest count
$(this).attr('name',$(this).attr('name').replace('rules\[0\]','rules\[$count\]'));
});
发布于 2014-03-08 05:19:44
尝试这样做:假设行属性名称为'filterCol‘,则可以为该属性设置新值
$("#trId").attr("filterCol", newValue);
发布于 2012-01-04 23:40:59
您应该使用$(this).attr('name')
而不是this.name
。
https://stackoverflow.com/questions/8729451
复制相似问题