簡體   English   中英

jQuery第一次克隆div,停止用戶克隆超過6次

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

我正在嘗試創建到現有div的克隆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());
    })

})

工作提琴: https : //jsfiddle.net/on3kj4hp/

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM