2012-07-23 1 views
1

복제 된 jQuery 객체로 div 컨텐츠 내의 각 id 속성에 대한 증가 값을 추가하는 방법을 파악하는 데 어려움이 있습니다.복제 된 jQuery 객체를 사용하여 div 컨텐츠 내의 각 id 속성에 증분 값을 추가하는 방법

http://jsfiddle.net/hvK8d/

===================== HTML =================== ==

<div class="upload-file-container"> 
    <div class="uploadFile left clearfix"> 
    <input type="file" id="FileUpload1"> 
    <table id="RadioButtonList1"> 
     <tbody> 
     <tr> 
      <td><input type="radio" value="Resume" id="RadioButtonList1_1"> 
      <label for="RadioButtonList1_1">Resume</label></td> 
      <td><input type="radio" value="Letter of Recommendation" id="RadioButtonList1_2"> 
      <label for="RadioButtonList1_2">Letter of Recommendation</label></td> 
      <td><input type="radio" value="Other" id="RadioButtonList1_3"> 
      <label for="RadioButtonList1_3">Other</label></td> 
     </tr> 
     </tbody> 
    </table> 
    </div> 
    <a href="javascript:;" class="remove left">Remove</a> </div> 
<div class=""><a class="plus" href="javascript:;">plus one</a></div> 

=====================의 JQUERY =================== == 대신 번호가 ID를 사용

//Cloning upload file control 
$('.remove').live('click', function() { 
    if (confirm("Are you sure you wish to remove this item?")) { 
     $(this).parent().slideUp('fast', function() { 
      $(this).remove(); 
     }); 
    } 
    return false; 
}); 

$('.plus').click(function() { 
    console.log('cloning'); 
    var divCloned = $('.upload-file-container:first').clone(); 
    divCloned.hide().insertAfter('.upload-file-container:last').slideDown('fast'); 
    return false; 
}); 
+0

많은 코드처럼 보이지 않습니다. 여기에 게시하고 바이올린 링크를 참조로 사용하는 것이 어떻습니까? jsfiddle이 폐쇄되면 어떨까요? 우리는 당신을 도울 수 없을 것입니다 :) – Lix

+0

당신이 무엇을 묻고 있는지 분명하지 않습니다. – jfriend00

+2

제게 당신이 왜 그것을 원하는지 분명하지 않습니다 - 예를 들어 name 속성이 없다는 것을 알아 두십시오. 불완전한가요? – Alexander

답변

3

완전한 설명을 위해 여기에 "템플릿"을 사용하는 작은 솔루션을 넣을 것입니다.

템플릿 숨기는 클래스 :

.upload-file-container.template { 
    display: none; 
} ​ 

대체 할 수있는 작은 기능 :

$.fn.template = function(variables) { 
    return this.each(function() { 
    this.innerHTML = this.innerHTML.replace(/{{(.+)}}/g, function(match, variable) { 
     return variables[variable]; 
    }); 
    return this; 
    }); 
}; 

템플릿 :

<div class="upload-file-container template"> 
    <div class="uploadFile left clearfix"> 
    <input type="file" id="FileUpload{{id}}"> 
    <table id="RadioButtonList{{id}}"><tbody> 
     <tr> 
     <td> 
      <input type="radio" value="Resume" id="RadioButtonList{{id}}_1"> 
      <label for="RadioButtonList{{id}}_1">Resume</label> 
     </td> 
     </tr> 
    </tbody></table> 
    </div> 
</div> 

사용법 :

var count = 0; 

var divCloned = $(".upload-file-container.template") 
    .clone() 
    .removeClass("template") 
    .template({ 
    id: count++ 
    }); 
,536을
+0

Alexander – Davis

+0

@ user952851, 방금'removeClass' 명령의 순서를 변경했습니다. – Alexander

2

, 당신은 name 속성에 배열과 같은 표기법 (예를 RadioButtonList[])를 사용하여 수, 그리고주위에 라벨을 포장한다s :

<td> 
    <label for="RadioButtonList1_1"> 
     <input type="radio" value="Resume" name="RadioButtonList1[]"> 
     Resume 
    </label> 
</td> 
<td> 
    <label for="RadioButtonList1_2"> 
     <input type="radio" value="Letter of Recommendation" name="RadioButtonList2[]"> 
     Letter of Recommendation 
    </label> 
</td> 
<td> 
    <label for="RadioButtonList1_3"> 
     <input type="radio" value="Other" name="RadioButtonList3[]"> 
     Other 
    </label> 
</td> 

P.S.RadioButtonList보다 자세한 이름을 사용하는 것도 고려해야합니다.