2013-06-11 6 views
0

다음 코드를 사용하여 단추를 눌러 확장 할 수있는 양식을 만들었지 만 입력 필드에 고유 한 이름을 추가하는 데 문제가 있습니다. 고유 한 ID를 추가하기 위해 노력했지만, 이름을 변경하면 작동이 멈췄습니다. 도움? . 더-이상 ID 속성을 설정복제 된 양식 요소에 고유 한 이름 추가

var clone = row.cloneNode(true); // copy children too 

    var theName= row.getAttribute('name'); 

    clone.setAttribute('name', theName + counter++);// change id or other attributes/contents 

답변

1

이 시도하지 name propertyElement의이 유형에는 적용되지 않습니다, 그래서 당신은 특별히 get the attribute을해야합니다.이 fiddle.

이름을 참조하십시오 취득 또는 DOM 개체의 이름 속성을 설정, 그것은 단지 다음과 같은 요소에 적용 <a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, <map>, <meta>, <object>, <param>, <select>,

var counter = 0; 

function cloneRow() { 
    var row = document.getElementById("rowToClone"),  // row to copy 
     table = document.getElementById("tableToModify"), // table to append to 
     clone = row.cloneNode(true),      // clone with children 
     theName = row.getAttribute('name') + ++counter; // get name attribute 
     // inc counter there, too 

    clone.setAttribute('name', theName); // re-set name 
    clone.setAttribute('id', theName); // set ID (lost during clone) 
    table.appendChild(clone);   // add new row to end of table 
} 
1

당신이있어, 그리고 counter과를 두 번 증가 당신은 지금이야 - : 어떤 조언을 :(많은 감사

var counter = 0 
function cloneRow() { 
    counter ++;  
    var row = document.getElementById("rowToClone"); // find row to copy 
    var table = document.getElementById("tableToModify"); // find table to append to 
    var clone = row.cloneNode(true); // copy children too 
    var theName= row.name; 
    clone.name = theName + counter++; // change id or other attributes/contents 
    table.appendChild(clone); // add new row to end of table 
} 
+0

+1 <textarea>.하지만, 더 나은 비보다 표준 속성을 사용하여 다시 갈 수있는 OP를 얻을 수 - 표준 하나. – RobG