简体   繁体   English

ngModel 和复选框/收音机无法正常工作

[英]ngModel and checkbox/radio not working properly

I made html template for my app.我为我的应用程序制作了 html 模板。 Basically it's a radio/checkboxes with text inputs which contain answers to questions.基本上它是一个带有文本输入的单选框/复选框,其中包含问题的答案。 It worked just fine until I've decided to add ngModel to them.在我决定将 ngModel 添加到它们之前,它工作得很好。 The thing is that when I add 2 or more answers and click on a label to set the correct one(/s) only the last one selects, moreover the answertext disappears whenever I click on label.问题是,当我添加 2 个或更多答案并单击 label 以设置正确的答案(/s)时,只有最后一个选择,此外,每当我单击 label 时,答案文本就会消失。

html text: html 文字:

 <div *ngIf="question.typeQuestions==1"> <div *ngFor="let item of question.selectedAnswer; let i = index" class="checkbox-variable"> <input type="checkbox" id="customCheckbox{{i}}" name="customCheckbox" class="checkbox-square" [(ngModel)]="item.isCorrect" > <label class="checkbox-label" for="customCheckbox{{i}}"><input class="checkbox-text" type="text" [(ngModel)]="item.text"></label> </div> </div>

ChrisY solved the problem. ChrisY 解决了这个问题。

having multiple input with the same name is definitive wrong here.具有相同名称的多个输入在这里是绝对错误的。 Try name="customCheckbox{{i}}" .试试name="customCheckbox{{i}}" When using ngModel you need a name that identifies the form control.使用 ngModel 时,您需要一个标识表单控件的名称。 It has to be unique它必须是独一无二的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM