Increase id with 1

Increase id with 1

本文关键字:with id Increase      更新时间:2023-09-26

>我想要以下内容:当用户单击按钮时,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

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

代码 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/

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

使用例如 相反find('input:text'),它会起作用。