2017-04-12 1 views
0

기존 div에 복제 div를 만들려고합니다. 클론이 작동하고 사용자가 버튼을 클릭하여 div를 복제 할 수 있습니다. 지금은의 메시지가 같은Jquery 복제 div 처음으로 6 회 이상 복제를위한 사용자 중지

<div id ="specdiv "> 
     <fieldset class="fieldset"> 
       <legend class="legend">Question Specification</legend> 
       <div class="editor-label"> 
      @Html.LabelFor(model => model.OfferedAnswer) 
     </div> 
      <div class ="answerchoice1" id=""> 
     <div class="editor-field"> 
      @Html.TextAreaFor(model => model.OfferedAnswer.AnswerText) 




     </div> 
       </div> 

      </fieldset> 


    </div> 
<button id="quesId" class="mini-button" type =" button">+</button> 

$(document).ready(function() { 
    $('button').click(function() { 
     //$('.answerchoice1').before($('.answerchoice1').clone()) 
     var $target = $('.answerchoice1').find('div.editor-field:first'); 
     $target.clone().appendTo('.answerchoice1'); 
     var tID = $(this).attr(".answerchoice1").split(/ _/); 
     //console.log($('.example-1').html()); 
    }) 

}) 

답변

1

뭔가 "당신은 더 이상 항목을 추가 할 수 없습니다"사용자가 유일하게 가능한 디스플레이 단지 6 번과 경우 DIV 위로를 복제 할 수있는 검증 될 싶은 것이 이것은 당신이 후에 무엇 :

$(document).ready(function() { 
    $('button').click(function() { 
     if($('.editor-field').length >= 6){ 
     alert('No more than 6!'); 
     return false; 
     } 
     //$('.answerchoice1').before($('.answerchoice1').clone()) 
     var $target = $('.answerchoice1').find('div.editor-field:first'); 
     $target.clone().appendTo('.answerchoice1'); 
     var tID = $(this).attr(".answerchoice1").split(/ _/); 
     //console.log($('.example-1').html()); 
    }) 

}) 

작업 바이올린 : https://jsfiddle.net/on3kj4hp/

는 희망이 도움이!

+0

감사합니다. – cedPound