【问题标题】:How can i create a dynamically input fields using bootstrap如何使用引导程序创建动态输入字段
【发布时间】:2018-02-04 14:53:04
【问题描述】:

我已经创建了输入字段,但是当我单击前两个时,效果很好,直到我要求第三个介于前两个之间,其他也介于两者之间。请帮帮我。 这是我的 javascript 代码:

<script type="text/javascript">

    $(document).ready(function() {

    it to after "after-add-more" div class.
      $(".add-more").click(function(){ 
          var html = $(".copy-fields").html();
          $(".after-add-more").after(html);
      });

      $("body").on("click",".remove",function(){ 
          $(this).parents(".control-group").remove();
      });

    });

</script>

【问题讨论】:

  • 为什么现在是这样,它甚至没有添加更多字段。请我需要更多帮助。

标签: javascript jquery input types submit


【解决方案1】:

$(document).ready(function() {
    $(".add-more").on('click', function(){ 
        var html = $(".copy-fields").clone();
        $(".after-add-more").after(html[0]);
    });

    $("body").on("click",".remove",function(){ 
        $(this).parents(".control-group").remove();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <button class = "add-more">ADD</button>
  <div class = "after-add-more"></div>
  <div class = "copy-fields"><input type = "text" /></div>
</div>

因为您试图复制 .copy-fields 类的 html 内容,所以请使用 clone() 代替 html(),然后使用 html[0] 进行插入,如下所示:

$(document).ready(function() {
    $(".add-more").click(function(){
        var html = $(".copy-fields").clone();
        $(".after-add-more").after(html[0]);
    });

    $("body").on("click",".remove",function(){ 
        $(this).parents(".control-group").remove();
    });
});

【讨论】:

  • 为什么它甚至没有添加更多字段?请我需要更多帮助
  • 感谢您的帮助,但我确实尝试过,但无法添加更多输入字段。请我需要更多帮助
  • 你好,你能提供你的html代码吗,你的html结构可能不同。
  • 我已经编辑了我的答案,并包含了一个有效的 sn-p 代码,请查看并与您的 html 代码进行比较。
  • @n.ssendawula 不要在别人的答案中发布你的代码。 您的代码属于您的问题,不属于其他人的问题。
猜你喜欢
  • 2019-05-28
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多