<form name="check-boxes">
<div *ngFor="let quest of noOfQuestions">
<div class="form-check form-check-inline" *ngFor="let ans of noOfAnswers">
<label class="form-check-label">{{ans}}
<input class="form-check-input" type="checkbox" name="{{quest}}" id="{{quest}}{{type}}" value="{{ans}}" (change)="getAnswers(id)">
<span class="form-check-sign"></span>
</label>
</div>
</div>
</form>
angular componentx.html 中的示例输出:
此代码根据提供的 noOfQuestions 和 noOfAnswers 生成一组复选框。当复选框被选中时,我想将复选框的特定 id 传递给函数“getAnswer”。 ts 文件中传递的参数“id”值表示未定义。 getAnswer() 的参数应该是什么来传递选择的复选框的 id ???
最佳答案
如果您通过 $event:
(change)="getAnswers($event)"
那么你应该可以通过
event.target
访问你的 typescript 文件中的那个输入元素但检查是否被选中也很重要:getAnswers(event) {
if (event.target.checked) {
console.log('checked: ' + event.target.id);
} else {
console.log('unchecked: ' + event.target.id);
}
}
关于html - 选中复选框时传递所选复选框的 id(已加载 ngFor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668156/