0

我想要以下内容:当用户单击按钮时,id 必须增加 1。现在我使用熟悉的函数来为我的行设置唯一 id,并且可以正常工作。现在我对唯一的输入字段使用了相同的代码片段,但这不起作用。同样奇怪的是,以下代码在 name 属性上正确工作。

    clone.find('#dsmeta_image_caption').prop('name', 'dsmeta_image_caption[' + row.length + ']'); // we now need to set the new ID for the row - ID's cannot be duplicated. This again will use the row.length

所以我想要的是将每个字段 id 增加 1。

代码JS:

function create_row_event(event) {
    // Declared variable
    var row = jQuery('tbody tr.row');
    var clone = row.last().clone(true);

    // Find the cloned fields and reset the values of it
    clone.find('input[type=text], text, textarea, select, input.upload_image').val(''); // Reset the values
    clone.find('.preview_image').attr('src', ''); // Reset the values


    row.parent('tbody.ui-sortable').append(clone); // Append the new row to the body
    clone.prop('id', 'repeatable-[' + row.length + ']'); // we now need to set the new ID for the row - ID's cannot be duplicated. This again will use the row.length
            clone.find('input#dsmeta_image_caption').prop('id', '[' + row.length + ']');


}

在这里你可以看到代码:http: //jsfiddle.net/Caspert/ttgMc/7/

4

1 回答 1

1

find('#id')操作将不起作用,因为那里涉及重复的 ID。一旦文档中有重复的 ID,您就不能期望基于 ID 的选择器/过滤器能够正常工作。

使用 egfind('input:text')代替它会起作用。

于 2013-02-01T21:09:43.703 回答