【问题标题】:Way to know which button was clicked - Angular 5知道点击了哪个按钮的方法 - Angular 5
【发布时间】:2018-05-05 13:00:04
【问题描述】:

我正在开发一个测验应用程序,对于给定的问题有 4 个选项。我已经编写了一个函数来验证单击的选项是正确答案还是错误。我在知道用户选择了哪个选项时遇到了问题,我想使用 CSS 将其设置为 - 如果选择了错误的选项,则单击选项会变成红色,正确的选项会变成绿色,反之亦然。

HTML:

<div *ngFor="let actiVar of activeArray ;let j = index" >

        {{actiVar.QuestionID}}.{{actiVar.Question}}
        <br>
        <br>
        <button type="button"  name="s" id="one" (click)="filterAnswer(actiVar.OP[j+0]) ; getColor(actiVar.OP[j+0])" [ngStyle]="{backgroundColor: buttonColor}" [disabled]="permissionoptions">{{actiVar.OP[j+0]}}</button>
        <br>
        <br>
        <button type="button"  name="s" id="two" (click)="filterAnswer(actiVar.OP[j+1]) ; getColor(actiVar.OP[j+1])" [ngStyle]="{backgroundColor: buttonColor}" [disabled]="permissionoptions">{{actiVar.OP[j+1]}}</button>        <br>
        <br>
        <button type="button"  name="s" id="three" (click)="filterAnswer(actiVar.OP[j+2]) ; getColor(actiVar.OP[j+2])" [ngStyle]="{backgroundColor: buttonColor}" [disabled]="permissionoptions">{{actiVar.OP[j+2]}}</button>        <br>
        <br>
        <button type="button"  name="s" id="four" (click)="filterAnswer(actiVar.OP[j+3]) ; getColor(actiVar.OP[j+3])" [ngStyle]="{backgroundColor: buttonColor}" [disabled]="permissionoptions">{{actiVar.OP[j+3]}}</button>
        <br>
      </div>

我已经设置了一个getColor func onclick 所选择的选项,但它的作用是,如果用户选择了错误的选项,它会将所有 4 个选项变为红色,反之亦然。它没有专门打开点击的选项变为红色。

getColor(j: any) { 
    if (j == this.activeArray[0].isRight) {

      this.buttonColor = 'green';
    }
    else {
      this.buttonColor = 'red';
    }
  }

this.activeArray[0].isRight 是从 JSON 中检索到的正确答案。 我知道我必须在按钮上使用单独的 id 标签才能知道单击了哪个选项按钮,但我没有运气在 stackoverflow 上找到正确的逻辑。

【问题讨论】:

    标签: javascript css angular


    【解决方案1】:

    您可以使用Template reference variables -> https://angular.io/guide/template-syntax#ref-vars

    <button #buttonRef1 type="button" (click)="filterAnswer(actiVar.OP[j+0], buttonRef1)" [disabled]="permissionoptions">{{actiVar.OP[j+0]}}</button>
    <button #buttonRef2 type="button" (click)="filterAnswer(actiVar.OP[j+1], buttonRef2)" [disabled]="permissionoptions">{{actiVar.OP[j+1]}}</button>
    <button #buttonRef3 type="button" (click)="filterAnswer(actiVar.OP[j+2], buttonRef3)" [disabled]="permissionoptions">{{actiVar.OP[j+2]}}</button>
    <button #buttonRef4 type="button" (click)="filterAnswer(actiVar.OP[j+3], buttonRef4)" [disabled]="permissionoptions">{{actiVar.OP[j+3]}}</button>
    

    过滤器答案:

    filterAnswer(answer: string, button: HTMLButtonElement) {
        // Do logic here
        button.style.backgroundColor = desiredColor; // example: "#f00"
    }
    

    【讨论】:

    • 这有点帮助!现在面临的问题是,当我单击 4 个选项中的任何选项时,它始终显示最后一个选项的颜色,通过验证单击的选项和 JSON 提供的答案。
    • @SanketSawant 您可以为引用使用不同的名称。我更新了答案。
    • 你救了我的命!我永远被困在这个按钮上。非常感谢你:)
    • @SanketSawant 很高兴听到这个消息 :)
    【解决方案2】:

    您可以使用自己的 filterAnswer 方法来传递按钮的名称。这种方法是更简单的方法。

     <button type="button"  name="s" id="one" (click)="filterAnswer(actiVar.OP[j+0], 'one') ; getColor(actiVar.OP[j+0])" [ngStyle]="{backgroundColor: buttonColor}" [disabled]="permissionoptions">{{actiVar.OP[j+0]}}</button>
    

    【讨论】:

    • 我在 getColor func 上试过这个,当我使用 console.log(j) 时,它只显示第一个值而不是'one'。
    【解决方案3】:

    您必须将 $event 作为另一个参数传递并从该对象获取 id

    检查这个解决方案

    Angular2 get clicked element id

    【讨论】:

    • 在发布这个问题之前我已经考虑过了,他们使用了浏览器的srcElement 属性,我不确定这是否是合适的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多