替换对象中字符的所有实例

时间:2012-11-20 16:06:40

标签: jquery replace

我克隆了一个选择器,但在输出之前,我希望用data_number替换data_number + 1的所有实例,但我遇到了麻烦。

有人可以告诉我我做错了什么吗?感谢。

var data_number = $('.data-fields').length, // The number of data rows
    data_row = $('.data-'+data_number),  // The last data row
    new_data_row = data_row.clone();        // A clone of the last data row

/** Replace all instances of the data_number with data_number + 1 */
???

/** Clear all of the input values in the clone */
$('input[type="text"]', new_data_row).val('');

/** Output the now clean cloned data row */
data_row.after(new_data_row);

例如,如果最初是new_data_row -

<div class="data-5">
    <input name="data[5][label]" />
    <input name="data[5][budget]" />
</div>

我希望它改为 -

<div class="data-6">
    <input name="data[6][label]" />
    <input name="data[6][budget]" />
</div>

3 个答案:

答案 0 :(得分:2)

快速而肮脏。 DOM - &gt; html - &gt;替换 - &gt; DOM。

var data_number = $('.data-fields').length, // The number of data rows
    data_row = $('.data-'+data_number),  // The last data row
    cloned_data = data_row.clone(),
    new_data_row,       // Raw html
    next_number = data_number + 1; 

cloned_data.find("input").val(""); //Clear inputs

new_data_row = cloned_data.wrap("<div/>").parent().html().replace(new RegExp(data_number + "", "gm"), next_number + "");

/** Output the now clean cloned data row */
data_row.after(new_data_row);

答案 1 :(得分:0)

您需要使用string replace,并且data_number + 1需要parseInt(data_number) + 1,除非您想在字符串的末尾添加1。

答案 2 :(得分:0)

好的,所以这里的答案比我希望的要长。

此外,这只能起作用,因为我手动替换了我知道需要替换的所有元素,所以如果有更好的方法,请分享。

<强>更新

我接受了另一个答案,因为它完全符合我的要求,所以尽管这段代码可行,但我建议你这样做。

$(document).ready(function(){

    var link = $('.add-data-link');     

    link.on('click', function(){

        var data_number = $('.data-fields').length, // The number of data rows
            next_number = data_number + 1,          // The next data number
            data_row = $('.data-'+data_number),     // The last data row
            new_data_row = data_row.clone(),        // A clone of the last data row
            class_name = 'data-'+data_number,       // The class of the cloned data
            new_class_name = 'data-'+next_number;   // The new class that will replace the cloned data

        /** Replace all instances of the 'data_number' with 'next_number' */
        var for_text, name_text;
        $(new_data_row).removeClass(class_name).addClass(new_class_name);
        $('label', new_data_row).each(function(){

            for_text = $(this).attr('for');
            for_text = for_text.replace(data_number, next_number);
            $(this).attr('for', for_text);

        });
        $('input', new_data_row).each(function(){

            name_text = $(this).attr('name');
            name_text = name_text.replace(data_number, next_number);
            $(this).attr('name', name_text);

        });

        /** Clear all of the input values in the clone */
        $('input[type="text"]', new_data_row).attr('value', '');

        /** Output the now clean cloned data row */
        data_row.after(new_data_row);

    });

});