2013-01-31 55 views
0

我想要以下內容。當用戶點擊按鈕時,ID必須增加1.現在我對我的行使用一個熟悉的函數來獲得唯一的ID,並且正常工作。現在我已經使用相同的代碼片段來處理唯一的輸入字段,但這不起作用。此外,它的奇怪的是,下面的代碼不正確的name屬性他的工作......用1增加ID

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/

回答

1

,因爲重複的ID參與那裏find('#id')操作將無法正常工作。只要文檔中存在重複的ID,就不能指望基於ID的選擇器/過濾器按照它們的工作方式工作。

使用例如相反,它會起作用。