Jquery 如何更改多个类的特定文本

Jquery 如何更改多个类的特定文本,jquery,attributes,replace,clone,selector,Jquery,Attributes,Replace,Clone,Selector,我正在克隆一个字段集,并在创建新克隆时增加属性名。但是,如果我删除我的克隆,我希望发生同样的情况,以便删除后的任何克隆字段集都将填补空白。如中所示,如果删除了第二个克隆,则将第三个克隆重命名为第二个克隆,以此类推 我正在尝试将所有以“_3”结尾的类和id更改为以“_2”结尾(或者类似地,将以“_2”结尾的类和id更改为以“_1”结尾)。目前,我正在使用jQuery分别更改每个特定的类或ID,我只是认为必须有一种更有效的方法来搜索它们,方法是过滤掉类或ID名称的下划线部分,并指定要更改的内容 正在

我正在克隆一个字段集,并在创建新克隆时增加属性名。但是,如果我删除我的克隆,我希望发生同样的情况,以便删除后的任何克隆字段集都将填补空白。如中所示,如果删除了第二个克隆,则将第三个克隆重命名为第二个克隆,以此类推

我正在尝试将所有以“_3”结尾的类和id更改为以“_2”结尾(或者类似地,将以“_2”结尾的类和id更改为以“_1”结尾)。目前,我正在使用jQuery分别更改每个特定的类或ID,我只是认为必须有一种更有效的方法来搜索它们,方法是过滤掉类或ID名称的下划线部分,并指定要更改的内容

正在克隆的我的HTML代码:

<fieldset class="basic_info">
    <h3 class="title_0">About You </h3>
                <div class="details">
                    <a class="delete driver_0" href="javascript:void(0);">Delete this driver</a>
                    <div class="proposer dob">
                        <label for="dob_0">Date of Birth</label><input type="text" name="dob_0" id="dob_0" /><span>Example: 30/07/1980</span>
                    </div>
                    <div class="proposer gender">
                        <span class="gender">Gender</span>
                        <input type="radio" name="gender_0" id="female_0" value="female" />
                        <label for="female_0">Female</label>
                    </div>
                    <div class="proposer gender">
                        <span class="invisible">&nbsp;</span>
                        <input type="radio" name="gender_0" id="male_0" value="male" />
                        <label for="male_0">Male</label>
                    </div>
                    <div class="proposer license_type">
                        <label for="license_type_0">License Type</label><select name="license_type_0" id="license_type_0"><option id="0">Please Select ...</option><option id="hide">Provisional</option><option id="show1">Full EU</option><option id="show2">Full Irish</option></select>
                    </div>
                    <div class="license_age_0 opt_show1 opt_show2">
                        <div>
                            <span>How long have you had your full license?</span>
                            <input type="radio" name="license_age_0" id="plus_five_yrs_0" value="female" />
                            <label for="plus_five_yrs_0">More than 5 Years</label>
                        </div>
                        <div>
                            <span class="invisible">&nbsp;</span>
                            <input type="radio" name="license_age_0" id="lessthan_five_yrs_0" value="male" />
                            <label for="lessthan_five_yrs_0">Less than 5 Years</label>
                        </div>
                    </div>

                </div>
            </fieldset>
谢谢,
ali

Jquery具有属性选择器,可用于执行StartWith和endswith操作

$(“id$=\u 2”)将是“id以\u 2结尾”的选择器

然后,您可以在结果列表上循环,并根据旧id设置新id

参考:

更新:

我开始担心你可能会让事情变得更复杂。如果在列表中添加或删除新元素时都必须这样做,那么您可能需要备份并考虑如何设置此页面的样式

话虽如此,我想到了更像这样的事情:

$("id$=_2").each( updateID );

//This'll only do IDs, but you can extned it to do the other attributes
function updateID()  
{
    //Jquery changes 'this' to point to the current 
    //element in the list when you use the 'each' method
    var old_id = $(this).attr("id");  //get the old id
    var new_id = old_id.replace("_2", "_3"); //update it
    $(this).attr("id", new_id);  //set it
}
但是,像这样尝试在这些元素上设置所有样式可能不是解决问题的正确方法。也许有更好的方法可以做到这一点,但是如果没有对问题的全面理解,没有看到HTML和CSS,就很难尝试正确的答案

$(fieldclone).find(id_2).each(function(){
   ("id_2").replaceWith.("id_1"); // THIS LINE ISN'T EXECUTING
});
$(fieldclone).find(id_3).each(function(){
   ("id_3").replaceWith.("id_2"); // THIS LINE ISN'T EXECUTING
});
我想到两件事:

  • 不执行的两行是那些使用
    $(fieldclone)
    作为选择器的行;这是否返回要操作的元素
  • 要使用('id_3')作为选择器,它应该包装在jQuery对象中,看起来更像
    $('id_3')

  • 另外,你说你试图实现@Chris Jaynes的答案,这在你发布的代码中似乎不可见,
    $('id_3')
    与他建议的
    $('id$=3')

    嗨,Chris-谢谢你给我看。我已经更新了我的原始帖子,试图采纳你的建议,但我认为我有语法问题。我认为你是对的。基本上,我有一个字段集,最多可以被克隆3次。我想我需要a)计算字段集数量的for循环,b)添加new/Clone函数,c)删除Clone函数,所以我想我需要从头开始重新构造javascript文件。我会记住你的建议——非常感谢!另外,天哪,我在jsbin.com/axowa/edit上添加了一个对这个代码版本不太满意的工作。。。我在这里要做的一个尝试:嗨,David-1:fieldclone返回一个包含所有元素的字段集。这些字段是从原始字段集克隆而来的,每个新克隆的属性都会增加一个。如果我要删除其中一个,比如说中间的克隆,我需要下面的其他克隆能够在队列中取代它们的位置。i、 e.如果我删除第二个克隆,第三个克隆的所有属性都需要成为命名的,就像现在删除的第二个克隆一样。2:我将尝试一下,看看在更新脚本的开始部分它是否工作良好,我有一些Veriable,它们描述了我希望以这种方式定位的每个属性-var id_1=$(“[id$=_1]”)。attr(“id”);在此之后,我一直在调用该变量。$(“[id$=\U 1]”)以整个元素为目标,而将其更改为:$(“[id$=\U 1]”)。attr('id')给了我id属性的实际文本。@Aligreen,fair dos,这些只是我想到的东西,它们太冗长了,无法作为注释发布。=)如果你想看一个例子,我可以给你发一个链接?
    $("id$=_2").each( updateID );
    
    //This'll only do IDs, but you can extned it to do the other attributes
    function updateID()  
    {
        //Jquery changes 'this' to point to the current 
        //element in the list when you use the 'each' method
        var old_id = $(this).attr("id");  //get the old id
        var new_id = old_id.replace("_2", "_3"); //update it
        $(this).attr("id", new_id);  //set it
    }
    
    $(fieldclone).find(id_2).each(function(){
       ("id_2").replaceWith.("id_1"); // THIS LINE ISN'T EXECUTING
    });
    $(fieldclone).find(id_3).each(function(){
       ("id_3").replaceWith.("id_2"); // THIS LINE ISN'T EXECUTING
    });