2017-04-12 65 views
0

我想創建一個克隆div到現有的div。克隆工作並允許用戶通過單擊按鈕來克隆div。現在我想什麼是驗證該用戶只允許克隆股利最多也只有6次,如果可能的話顯示「你不允許添加了項目」的消息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

喜歡的東西這是你所追求的:

$(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