【问题标题】:Jquery clone div for the first times, stop users for cloning more than 6 timesjquery第一次克隆div,6次以上停止用户克隆
【发布时间】:2017-04-12 12:53:13
【问题描述】:

我正在尝试为现有 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());
    })

})

【问题讨论】:

    标签: javascript jquery html clone


    【解决方案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/

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2014-02-21
      • 2019-04-20
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多