动态创建表单上的单击按钮不起作用

时间:2017-02-15 08:04:50

标签: javascript jquery twitter-bootstrap

我正在尝试通过单击功能动态创建Bootstrap Panel,但是每个Panel都应该通过单击功能创建多个表单。在我的代码中,第一个Panel通过单击按钮创建表单,但其余的面板不会创建表单..



    var counter=1;
    $(".panel-increment").on("click", function(e){
       e.preventDefault();
        var panelHtml = '<div class="panel panel-info"> ' +
                '<div class="panel-heading"> ' +
                '<h4 class="panel-title"> ' +
                '<a data-toggle="collapse" data-parent="#accordion" href="#collapse' + (counter + 1) + '">Day ' + (counter + 1) + '<i class="fa fa-plus pull-right" aria-hidden="true"></i></a></h4>' +
                '</div> ' +
                '<div id="collapse' + (counter + 1) + '" class="panel-collapse collapse"> ' +
                '<div class="panel-body">' +
                '<div class="row" > ' +
                '<div class="col-md-12 sch-box">' +
                '<div class="col-md-6 form-group"><input type="time" class="form-control" value="00:00" id="sch-s-time" name="sch-s[]"></div> ' +
                '<div class="col-md-6 form-group"><input type="time" class="form-control" value="00:00" id="sch-e-time" name="sch-e[]"></div> ' +
                '<div class="col-md-12 form-group"><input type="text" class="form-control" placeholder="Enter Description" id="sch-title" name="sch-title[]"></div> ' +
                '<div class="col-md-12 form-group"><textarea id="sch-title" class="form-control vresize" name="sch-title[]"></textarea></div> ' +
                '</div> ' +
                '</div> ' +
                '</div> ' +
                '<button type="button" class="btn center-block panel-addbtn" ><i class="fa fa-plus"></i> </button> ' +
                '</div> ' +
                '</div>';
        $(this).closest(".it-right-side").find(".panel-group").append(panelHtml);
        counter++;
    });
 
    $(".panel-addbtn").on("click", function(e){
        e.preventDefault();
        var formHtml = '<div class="row" >' +
                '<div class="col-md-12 sch-box"> ' +
                '<div class="col-md-6 form-group"><input type="time" class="form-control" value="00:00" id="sch-s-time" name="sch-s[]"></div>' +
                '<div class="col-md-6 form-group"><input type="time" class="form-control" value="00:00" id="sch-e-time" name="sch-e[]"></div>' +
                '<div class="col-md-12 form-group"><input type="text" class="form-control" placeholder="Enter Description" id="sch-title" name="sch-title[]"></div>' +
                '<div class="col-md-12 form-group"><textarea id="sch-title" class="form-control vresize" name="sch-title[]"></textarea></div>' +
                '</div>' +
                '</div>';
        $(this).closest(".panel-collapse").find(".panel-body").append(formHtml);
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-6 it-right-side">
  <div>
    <div class="col-md-12" id="dynamicInput">
      <!--Start Panel-->
      <div class="panel-group" id="accordion">
          <div class="panel panel-info">
             <div class="panel-heading">
               <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">Day 1
<i class="fa fa-plus pull-right" aria-hidden="true"></i></a></h4>
             </div>
             <div id="collapse1" class="panel-collapse collapse">
               <div class="panel-body" id="formcreation">
                <!--Schedule-->
                 <div class="row" >
                  <div class="col-md-12 sch-box">
                    <div class="col-md-6 form-group"><input type="time" class="form-control" value="00:00" id="sch-s-time" name="sch-s[]"></div>
                   <div class="col-md-6 form-group"><input type="time" class="form-control" value="00:00" id="sch-e-time" name="sch-e[]"></div>
                   <div class="col-md-12 form-group"><input type="text" class="form-control" placeholder="Enter Description" id="sch-title" name="sch-title[]"></div>
                   <div class="col-md-12 form-group"><textarea id="sch-title" class="form-control vresize" name="sch-title[]"></textarea></div>
            </div>
          </div>
       </div>
<button type="button" class="btn center-block panel-addbtn" ><i class="fa fa-plus"></i> </button>
    </div>
   </div>
 </div>
</div>
<!--End panel-->
</div>
 <button type="button" class="btn center-block panel-increment"><i class="fa fa-plus"></i> </button>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

这是因为:

$("selector").on("click", function(){

});

适用于静态html,如果使用动态html .on(),如:

$("document").on("click", ".panel-addbtn", function() {

});

答案 1 :(得分:1)

您可以尝试在动态html中定义onclick事件,例如

<div onclick="myFunction()">
</div>

现在你可以创建一个javascript函数,现在它总能工作,因为html是静态的或动态的

function myFunction(){
//do something
}