我正在用角度創建一個測驗應用程式。我正在從 api 中獲取資料,其中有國家及其各自的首都。我隨機獲取它們并將它們映射為問題和答案。現在我想計算分數。為此我是試圖獲取所選按鈕的值。我正在通過 document.getelementbyID("idx") 執行此操作,但這會回傳任何隨機選項而不是我選擇的選項。任何人都可以幫助我嗎
correct() {
var y= document.getElementById("idx")
console.log(y)
if(y==this.saveCap[this.userIndex]){
this.score=this.score 1;
console.log(true)
console.log(this.score)
// this.userIndex ;
}
}
checkoptions(){
this.correct();
this.userIndex
// this.options.remove(this.x)
this.arrayDel();
this.arrayform();
this.shuffle();
}
HTML檔案
<h1>Quiz</h1>
<div *ngIf="saveCont && saveCap " class="quescard">
<form >
<h1>What is the capital of{{saveCont[userIndex]}}</h1><br>
<li *ngFor="let cap of options"><button type="button" class="btn btn-primary btn-block btn-lg" (click)="checkoptions()" id="idx" >{{cap}}</button></li>
<button (click)="res()">Submit</button>
</form>
</div>
uj5u.com熱心網友回復:
因為 id 被添加到兩個按鈕,所以它不會讓你點擊緊按鈕。您需要cap像這樣將 傳遞給點擊事件:
<li *ngFor="let cap of options"><button type="button" class="btn btn-primary btn-block btn-lg" (click)="checkoptions(cap)" id="idx" >{{cap}}</button></li>
然后在checkoptions函式內部得到它
checkoptions(cap: string){
// take the answer and check it and increase the score if it's correct
this.correct(cap);
this.userIndex
// this.options.remove(this.x)
this.arrayDel();
this.arrayform();
this.shuffle();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534974.html
