vendredi 28 mai 2021

how to send checkbox checked value along with its specific question id in angular

I have a requirement in my project. I have a set of questions which has text, radio, check boxes as options to select and answer on Submit form. I am using below code in my template for checkbox:

<form #form="ngForm" (ngSubmit)="submit(form)">
...
<div *ngFor="let option of question.options">
                    <input type="checkbox" [(ngModel)]="option.Checked" [name]="question.id" value=""
                    (change)="change($event,option.name)">
                
</div>
...
</form>

in my ts file I have code :

 this.testForm = this.fb.group({
      answers : this.fb.array([])
})

now on submit I am calling:

submit(f) {
this.testForm.value.answers.push(f.value)
}

but I need to submit the form as :

answers :[{"questionId": "yes", "questionId2" : "checkbox1 value,checkbox2 value", "questionId3" : "no"}]

where questions id is respective questions id from json, "yes" is selected radio button value for questionId2, and so on.

I am able to get radio button, text box but stuck with check box logic.




Aucun commentaire:

Enregistrer un commentaire