简体   繁体   中英

ngModel and checkbox/radio not working properly

I made html template for my app. 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. 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.

html text:

 <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.

having multiple input with the same name is definitive wrong here. Try name="customCheckbox{{i}}" . When using ngModel you need a name that identifies the form control. It has to be unique

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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