如何动态追加到2列

时间:2018-02-22 12:46:43

标签: javascript jquery html

我有很多输入,我想要一个解决方案来创建一个包含2列的行,然后逐个添加输入(因此除非它们是impar的,否则每边50%)。

Atm正在创建许多行。我该怎么做?

$.each(inputs, function (index, input) {
    $(".modal-body").append('<div class="row"><div class="col-sm-6"></div><div class="col-sm-6"></div></div>');
 }

1 个答案:

答案 0 :(得分:1)

您可以使用.wrap(),然后使用.after()

&#13;
&#13;
$('input[type="text"]').wrap('<div class="first-col"></div>');
$('.first-col').after('<div class="second-col"></div>');
&#13;
*{
  margin: 0;
  padding: 0;
}

.first-col{
  display: inline-block;
  background: blue;
  height: 30px;
  width: 200px;
  margin: 20px 0 20px 0;
}

.second-col{
  display: inline-block;
  width: 100px;
  height: 20px;
  background: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="fst"/><hr/>
<input type="text" id="snd"/><hr/>
<input type="text" id="trd"/><hr/>
<input type="text" id="frt"/><hr/>
&#13;
&#13;
&#13;