我正在尝试循环并根据存储在数组中的值附加多个容器,然后相应地设置这些值,但我目前正在复制这些值,并且无法在每次循环时获取不同的值循环。
var arr_tele = ['02991812376', '02982919291'];
//Prevent Duplicates.
$(".teledivcontain").remove();
//Append Container for numbers
$("#telediv").append('<div class="form-group col-md-3" id="teledivcontain"> </div>');
//Loop and append fields for each number
for (i in arr_tele) {
$("#teledivcontain").append('<input type="text" class="form-control telenumber" placeholder="No number currently" disabled><div class="form-group col-md-3"><button type="submit" class="btn btn-primary form-control detach">Detach</button></div>').find('input:text').val(arr_tele[i]);
}
.fieldpos {
margin-left: 45px;
width: 40%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="panel-body row fieldpos">
<fieldset class="form-group">
<label for="telenum">Your Telephone Numbers</label>
<div class="row">
<div id="telediv">
</div>
</div>
</fieldset>
</div>
我遗漏了一些未成年人但却无法看到任何人的想法?
答案 0 :(得分:2)
你必须找到附加的最后一个输入然后分配值,所以只需添加:last
选择器就能完成工作:
.find('input:text:last').val(arr_tele[i])
或者您可以直接使用<input value="'+arr_tele[i]+'" ...
分配值。
希望这有帮助。
var arr_tele = ['02991812376', '02982919291'];
//Prevent Duplicates.
$(".teledivcontain").remove();
//Append Container for numbers
$("#telediv").append('<div class="form-group col-md-3" id="teledivcontain"> </div>');
//Loop and append fields for each number
for (i in arr_tele) {
$("#teledivcontain").append('<input type="text" class="form-control telenumber" placeholder="No number currently" disabled><div class="form-group col-md-3"><button type="submit" class="btn btn-primary form-control detach">Detach</button></div>').find('input:text:last').val(arr_tele[i]);
}
.fieldpos {
margin-left: 45px;
width: 40%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="panel-body row fieldpos">
<fieldset class="form-group">
<label for="telenum">Your Telephone Numbers</label>
<div class="row">
<div id="telediv">
</div>
</div>
</fieldset>
</div>
答案 1 :(得分:1)
只需在HTML中添加属性即可添加。请注意循环'<input type="text" ... value="' + arr_tele[i] + '"disabled>
:
var arr_tele = ['02991812376', '02982919291'];
//Prevent Duplicates.
$(".teledivcontain").remove();
//Append Container for numbers
$("#telediv").append('<div class="form-group col-md-3" id="teledivcontain"> </div>');
//Loop and append fields for each number
for (i in arr_tele) {
$("#teledivcontain").append('<input type="text" class="form-control telenumber" placeholder="No number currently" value="' + arr_tele[i] + '"disabled><div class="form-group col-md-3"><button type="submit" class="btn btn-primary form-control detach">Detach</button></div>');
}
&#13;
.fieldpos {
margin-left: 45px;
width: 40%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="panel-body row fieldpos">
<fieldset class="form-group">
<label for="telenum">Your Telephone Numbers</label>
<div class="row">
<div id="telediv">
</div>
</div>
</fieldset>
</div>
&#13;