1

我克隆了一个选择器,但在输出它之前,我希望用 替换所有实例data_numberdata_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>
4

3 回答 3

2

又快又脏。DOM --> html --> 替换 --> 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);
于 2012-11-20T16:23:48.150 回答
0

Ok, so the answer here was longer than I'd hoped.

Also, this only works because I am manually replacing all the elements that I know need replacing, so if there are any better methods, please do share.

Update

I've accepted another answer because it achieves exactly what I want, so although this code will work, I'd recommend that.

$(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);

    });

});
于 2012-11-20T17:11:14.357 回答
0

您需要使用字符串替换,除非您想将 1 附加到字符串的末尾,否则您需要这样做data_number + 1parseInt(data_number) + 1

于 2012-11-20T16:19:39.383 回答