jQuery:查找和替换属性值 [英] JQuery: find and replace of attribute values

查看:53
本文介绍了jQuery:查找和替换属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查找和替换属性值时遇到困难.

I am having difficulty performing a find and replace of attribute values.

考虑此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>

我想用适当的计数来更新每个的name属性,例如rules [0] .isActive已更新为rules [10] .isActive

I am looking to update each 's name attribute with the appropriate count e.g. rules[0].isActive is updated to rules[10].isActive

我正在使用以下JQuery代码段:

I am using this JQuery snippet:

$(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"是未定义的.

where newRow is a variable holding the entire block and count holds the latest counter value. However, when I debug this code it seems that "this.name" is undefined.

我在哪里错了?

谢谢

推荐答案

要更新元素的属性,应使用.attr函数.另外,在jQuery上下文中使用this时,必须使用$(this)语法.如果我了解您的意图,那么应该是这样:

To update attribute of element you should use the .attr function. Also, when using this in jQuery context you must use $(this) syntax. If I understand your intentions so it should be something like that:

$(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\]'));
}); 

这篇关于jQuery:查找和替换属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆