我必须使用jquery动态添加div的内容。
这些元素的代码如下:
<nav class="choose_option" id="navch">
{{ form_widget(form.critere1) }}
<select id="select1">
<option value="normal" class="highlight_orange">Normal</option>
<option value="nombre">Nombre</option>
</select>
<select id="op1">
<option value="like" class="highlight_orange">Like</option>
<option value="not like">NOT LIKE</option>
<option value="=">=</option>
<option value="<"><(inf)</option>
<option value=">"><(sup)</option>
<option value="<>"> <></option>
</select>
<input type="text" id="txtcrit1" {{ app.request.get('txtcrit1') }}/>
</nav>
<nav>
<ul class="list">
<li class="list__item left pushright">
<label class="label--checkbox">
<input type="radio" name="radio" value="AND" class="checkbox" id="chx1">
ET
</label>
</li>
<li class="list__item left pushright">
<label class="label--checkbox">
<input type="radio" name="radio" value="OR" class="checkbox" id="chx2">
OU
</label>
</li>
</ul>
</nav>
我的问题,我如何使用jquery和javascript以这种方式动态添加相同的代码当我选择五个或十个选项时,我得到五个或几十个元素
答案 0 :(得分:2)
我已经为你做了一个如何做到这一点的简单例子:
$( document ).ready(function()
{
$('#selectBox').on('change', function()
{
switch (this.value)
{
case "5":
case "10":
for (i = 0; i < this.value; i++)
{
$("#appendToMe").append('<nav class="choose_option" id="navch">{{ form_widget(form.critere1) }}<select id="select1"><option value="normal" class="highlight_orange">Normal</option><option value="nombre">Nombre</option></select><select id="op1"><option value="like" class="highlight_orange">Like</option><option value="not like">NOT LIKE</option><option value="=">=</option><option value="<"><(inf)</option><option value=">"><(sup)</option><option value="<>"> <></option></select><input type="text" id="txtcrit1" {{ app.request.get("txtcrit1") }}/></nav><nav><ul class="list"><li class="list__item left pushright"><label class="label--checkbox"><input type="radio" name="radio" value="AND" class="checkbox" id="chx1">ET</label></li><li class="list__item left pushright"><label class="label--checkbox"><input type="radio" name="radio" value="OR" class="checkbox" id="chx2">OU</label></li></ul></nav>');
}
break;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="selectBox">
<option>select option</option>
<option>5</option>
<option>10</option>
</select>
<div id="appendToMe">
</div>
答案 1 :(得分:1)
use $(selector).append() to append data