2009-08-04 10 views
2

그래서이 코드가테이블 행 복제, ID에 임의 번호 또는 순차 번호 추가

$('input.clone').live('click', function(){ 
    //put jquery this context into a var 
    var $btn = $(this); 
    //use .closest() to navigate from the buttno to the closest row and clone it 
    var $clonedRow = $btn.closest('tr').clone(); 
    //append the cloned row to end of the table 

    //clean ids if you need to 
    $clonedRow.find('*').andSelf().filter('[id]').each(function(){ 
     //clear id or change to something else 
     this.id += '_clone'; 
    }); 

    //finally append new row to end of table 
    $btn.closest('tbody').append($clonedRow); 
}); 

문제는, 각 행을 내가 복제 얻을 이름이 무엇이든 _clone 어떻게 쓸 것이라고 나는 함수를 실행할 때마다 임의 또는 더 이상 idealy 일련 번호를 선택합니다. "_clone"의 ID isntead에 추가하십시오.

+0

의 행의 수를 사용할 수 있습니까? 그들은 row_x 수 있다면 – redsquare

+0

글쎄, 내가 그들을 DB에서 당겨, 메신저 순차적 인 번호를 그들에게 이름을 PHP를 사용하려고 – mrpatg

+0

또한 당신은 그것의 ID가 tr의 확신합니까? – redsquare

답변

1
String(Math.floor(Math.random()*1000000)) 

당신에게 문자열 형태의 임의의 숫자를 줄 것이다 "_clone"의 id isntead에 추가 할 임의의 이상 idealy 일련 번호 중 하나를 선택합니다 0 ~ 999999

1

왜 테이블로 시작하는 아이디의이 무엇인지

$clonedRow.find('*').andSelf().filter('[id]').each(function(){ 
     //clear id or change to something else 
     var rowCount = $btn.closest('tr').siblings().length; 
     this.id += 'row_' + rowCount; 
    }); 
0
$('input.clone').live('click', function(){ 
    var $btn = $(this); 
    var $clonedRow = $btn.closest('tr').clone(); 

    $clonedRow.find('*').andSelf().filter('[id]').each(function(){ 
     this.id += '_clone' + $(this).siblings().size(); 
    }); 

    //finally append new row to end of table 
    $btn.closest('tbody').append($clonedRow); 
}); 
+0

이고 크기가 .length (size()가 실제로는 .length를 내부적으로 호출 함)보다 느립니다. – redsquare