[英]Jquery use value of for loop to append to id attribute
I am making quiz like app.我正在像应用程序一样进行测验。 For every question, it will have 4 answers.
对于每个问题,它将有 4 个答案。 Then I will use radio buttons to indicate the right answer.
然后我将使用单选按钮来指示正确答案。 How can I append the value of the loop to the id attribute?
我如何才能将 append 的值循环到 id 属性?
Thank youuu谢谢你
Here is my code snippet:这是我的代码片段:
$('#num_qu').on('change', function(){
var num_of_qu = $('#num_qu').val();
var html = "";
for (var i = 0; i < num_of_qu; i++){
html+= '<div class="row"><div class="form-row"><div class="form-group col-md-6">
<label>Name of question</label><input type="text" class="form-control" name="qu_name"
required></div></div></div>';
for (j = 1; j < 5; j++){//4 answers
<div class="row">
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" class="form-control" id=answer_j placeholder="Answer"
required>
</div>
</div>
</div>
}
}
As per our discussion, this answer works out for your needs:根据我们的讨论,这个答案可以满足您的需求:
$("#num_qu").on("change", function () {
var num_of_qu = $("#num_qu").val();
var html = "";
for (var i = 0; i < num_of_qu; i++) {
html += `<div class="row"><div class="form-row"><div class="form-group col-md-6">
<label>Name of question</label><input type="text" class="form-control" name="qu_name"
required></div></div></div>`;
for (j = 1; j < 5; j++) {
//4 questions
html += `<div class="row">
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" class="form-control" id=answer_${j} placeholder="Answer"
required>
</div>
</div>
</div>`;
}
}
});
I just use template strings to place the value of j at the id attribute of the text input.我只是使用模板字符串将 j 的值放在文本输入的 id 属性中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.