【问题标题】:How make simple div box repeater jquery?如何制作简单的 div 框中继器 jquery?
【发布时间】:2018-10-17 21:39:32
【问题描述】:

如何使用 jquery 制作简单的 div 框中继器

> <div class="mybox"> <input name="name[]"  /> <input name="number[]" 
> /> <input name="text[]"  /> </div>and  

我有带字段的 div 框,我需要我可以用 + 添加相同的 div 框 喜欢这张照片

【问题讨论】:

标签: jquery html css


【解决方案1】:

您可以创建输入元素的模板并将其添加到父容器。我已经创建了下面的模板。

var content = "<div class='container'><input type='text'/><input type='number' /><input type='text'/><button type='button' id='remove'>-</button></div>";

var content = "<div class='container'><input type='text'/><input type='number' /><input type='text'/><button type='button' id='remove'>-</button></div>";

$(".parent-container").html(content);

$("#add").on("click", function(){
	if($(".parent-container").children().length <5){
  	$(".parent-container").append(content);
  }
  if($(".parent-container").children().length == 5){
  	$("#add").hide();
  }
});

$(".parent-container").on("click", "#remove", function(){
	$(this).parent().remove(); 
  	$("#add").show();
});
.parent-container{
  height:auto;
}

.container{
  border:1px solid lightblue;
  width:180px;
  margin:5px 0;
  position:relative;
}

.container > input {
  display:block;
  margin:5px 0;
}

.container > button {
  position: absolute;
  top: 30px;
  left: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent-container">

</div>
<button type='button' id='add'>+</button>

测试一下here

更新 1:

最多加 5.. 测试一下here

【讨论】:

  • 怎样才能最大+5次
  • 我在这个脚本标签中添加了 JavaScript 但它不起作用
  • 我已经更新了答案。如果您认为它解决了您的问题,请接受它,以便对其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 2012-01-19
  • 2012-02-08
相关资源
最近更新 更多