[英]How to retrieve the attribut value
I'm trying to push value of checkbox inside the database我正在尝试在数据库中推送复选框的值
I'm using two files (html and ts)我正在使用两个文件(html 和 ts)
This is my survey-result.ts:这是我的调查结果.ts:
export class SurveyResultIdComponent implements OnInit {
id: string = "";
surveyDetail = [];
surveyAnswer: FormGroup;
constructor(private router:Router, public route: ActivatedRoute, private fb: FormBuilder) {
this.id = this.route.snapshot.paramMap.get('id'); //get id parameter
}
ngOnInit() {
this.getSurveyDetail();
this.surveyAnswer = this.fb.group({
survey: ['test',Validators.required],
question: ['survey',Validators.required],
choices: ['choix1',Validators.required],
})
}
getSurveyDetail() {
const apiUrl = 'http://localhost:3000/api';
axios({
method: 'get',
url: `${apiUrl}/survey/show/`+ this.id,
data: this.surveyDetail
}).then((response) => {
this.surveyDetail = response.data;
});
}
answer() {
const apiUrl = 'http://localhost:3000/api'
axios({
method: 'post',
url: `${apiUrl}/survey/answer`,
data: this.surveyAnswer.value
})
}
onSubmit(form:FormGroup){}
}
And here this is my survey-result.html:这是我的调查结果。html:
<tr *ngIf="surveyDetail">
<td>{{ surveyDetail.name }}</td>
<td>{{ surveyDetail.questionnaires[0].question }}</td>
<td>
<p>
<label>
<input type="checkbox" formControlName="choices" value='{{ surveyDetail.questionnaires[0].choices[0].text }}'/>
<span>{{ surveyDetail.questionnaires[0].choices[0].text }}</span>
</label>
</p>
</td>
<td>
<p>
<label>
<input type="checkbox" formControlName="choices" value='{{ surveyDetail.questionnaires[0].choices[0].text1 }}'/>
<span>{{ surveyDetail.questionnaires[0].choices[0].text1 }}</span>
</label>
</p>
</td>
</tr>
</tbody>
</table>
<div class="answer">
<button class="btn waves-effect waves-light" type="button" name="action" (click)="answer()">Answer
<i class="material-icons right">add</i>
</button>
So I have a question ( Do you like juice?)所以我有一个问题(你喜欢果汁吗?)
You have 2 checkbox (Yes And No) The problem is when I choose a checkox and then click on answer.您有 2 个复选框(是和否)问题是当我选择一个复选框然后单击答案时。 I have 'choix1' and not the value of checkox我有 'choix1' 而不是 checkox 的值
I think my problem is maybe due to set a value?我认为我的问题可能是由于设置了一个值?
Here you have the structur if this can help you:如果这可以帮助您,您可以在这里获得结构:
So based on the selection your control will have a different value, this feels like radio button job, not a checkbox, I would do it like this.因此,根据您的选择,您的控件将具有不同的值,这感觉就像单选按钮工作,而不是复选框,我会这样做。
<label>
<input type="radio" [value]="surveyDetail.questionnaires[0].choices[0].text" formControlName="choices">
<span>{{surveyDetail.questionnaires[0].choices[0].text}}</span>
</label>
<label>
<input type="radio" [value]="surveyDetail.questionnaires[0].choices[0].text1" formControlName="choices">
<span>{{surveyDetail.questionnaires[0].choices[0].text}}</span>
</label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.