Jquery第一次克隆div,克隆超过6次停止用户

Jquery clone div for the first times, stop users for cloning more than 6 times

我正在尝试为现有 div 创建一个克隆 div。克隆正在运行并允许用户通过单击按钮克隆 div。现在我想做一个验证,用户最多只能克隆 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());
    })

})

你想要的就是这样的东西:

$(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());
    })

})

工作fiddle: https://jsfiddle.net/on3kj4hp/

希望对您有所帮助!