【问题标题】:ionic2 how to select only one check box and get the selected check box valueionic2如何只选中一个复选框并获取选中的复选框值
【发布时间】:2017-10-04 19:27:25
【问题描述】:

我有一个屏幕,其中有大约 20 个问题和答案,这些问题和答案将来自 db。所以我使用复选框显示选项。直到现在我才找到解决方案。

我需要的是,每个问题我只需要选中 4 个复选框中的一个。并且需要获取选中复选框的值。这是我的代码:

在我的 .js 中,我需要做什么才能获得选中的值,并且在复选框的 4 个选项中只选择一个复选框。

提前致谢!!

完整的html代码:

 <ion-header >
     <ion-navbar color="navcolr" no-border-bottom>
     <ion-buttons>
     <button (click)="canceltap()" style="font-size: 15px;text-transform: none;" ion-button>Cancel
        </button>
        </ion-buttons>
      <ion-title >Mock Test</ion-title>

      <ion-buttons end>
          <button ion-button type="submit" (click)="finished()" block>
            <ion-icon style="font-size: 15px;">Submit</ion-icon>
          </button>
        </ion-buttons>
     </ion-navbar>
    </ion-header>

     <ion-content fullscreen>

        <ion-card *ngFor="let card of subdata">

          <ion-item>
    <div class="item-text-wrap" style="text-align: center;font-weight: 400;font-size: 15px;">

      <label>Questions: </label><label class="item-text-wrap" style="color: #303030;">{{card.Question}}</label>
    </div>


    <div style="height: 1px;background-color: #696969;margin-left: 4%;margin-right: 4%;margin-top: 3%;"></div>
    <div style="margin-top: 3%;">
      <img src="http://www.addictedtoibiza.com/wp-content/uploads/2012/12/example.png">
    </div>


          </ion-item>
           <ion-card-content>

           <div style="margin-left: 4%;margin-top: 6%;">
    <ol id="options" type="A" >
       <div style="width: 10px;"></div>
          <li style="margin-top: 5%;">
            <label>
              <input name="question1" type="radio" value="{{card.Answer1}}" required>
            {{card.Answer1}}
            </label>
          </li>
          <li style="margin-top: 5%;">
            <label>
              <input name="question1" type="radio" value="{{card.Answer2}}">
           {{card.Answer2}}
            </label>
          </li>
          <li style="margin-top: 5%;">
            <label>
              <input name="question1" type="radio" value="{{card.Answer3}}">
              {{card.Answer3}}
            </label>
          </li>
          <li style="margin-top: 5%;">
            <label>
              <input name="question1" type="radio" value="{{card.Answer4}}" >
          {{card.Answer4}}
            </label>
          </li>
        </ol>


     </div>

    <button ion-button full round id="Ansbtn" (click)="onButtonClick()" [disabled]="!isenabled">View Answer/Description</button>


     <div class="item-text-wrap" *ngIf="buttonClicked" style="text-align: center;font-weight: 400;font-size: 15px;">
     <div style="height: 1px;background-color: #696969;margin-left: 4%;margin-right: 4%;margin-top: 3%;"></div>

    <label  style="color: #303030;">Answer: {{card.CorrectAnswer}}</label><br>
    <label class="item-text-wrap" style="color: #303030;">Explanations: {{card.AnsDescription}}</label>
    <div style="height: 1px;background-color: #696969;margin-left: 4%;margin-right: 4%;margin-top: 3%;"></div>
    </div>

          </ion-card-content>
        </ion-card>

    <div style="height: 10px;background-color: #ededed;"> 

    </div>
    <div style="height: 30px;text-align: center;" *ngIf="buttonClicked"> 
      <label  style="color: red;font-size: 18px;">SCORE: 1</label><br>
    </div>

    <div style="height: 30px;background-color: #ededed;"> 

    </div>



</ion-content>

【问题讨论】:

  • 如果用户只需要从几个选项中选择一个,请使用单选按钮。为它们提供相同的名称并使其成为必需项。
  • @Berend de Groot 我尝试使用单选按钮,但我在滚动页面中有 10 个问题。所以如果用户选择第一个选项,我的第一个选项就是选择。那么如果用户为第二个问题选择任何选项,第一个问题选择的答案将自动取消选中

标签: html angularjs ionic-framework ionic2 hybrid-mobile-app


【解决方案1】:

我会将所有复选框更改为单选输入,为它们赋予相同的名称并为它们添加一个实际值。我还在您的第一个输入中添加了“必需”属性,因此他们应该至少选择一个单选按钮。

假设这是第一个问题:

<ol id="options" type="A" >
   <div style="width: 10px;"></div>
      <li style="margin-top: 5%;">
        <label>
          <input name="question1" type="radio" value="{{card.Answer1}}" required>
        {{card.Answer1}}
        </label>
      </li>
      <li style="margin-top: 5%;">
        <label>
          <input name="question1" type="radio" value="{{card.Answer2}}">
       {{card.Answer2}}
        </label>
      </li>
      <li style="margin-top: 5%;">
        <label>
          <input name="question1" type="radio" value="{{card.Answer3}}">
          {{card.Answer3}}
        </label>
      </li>
      <li style="margin-top: 5%;">
        <label>
          <input name="question1" type="radio" value="{{card.Answer4}}" >
      {{card.Answer4}}
        </label>
      </li>
    </ol>

现在因为所有这 4 个输入字段都具有相同的名称,并且因为它是一个单选按钮,所以只能选择其中一个。现在第二个问题将与上面相同,只是名称不同。

【讨论】:

  • 我是否需要在我的.js 文件中添加任何额外的代码
  • 不,应该够了。
  • 我试过你的解决方案。但是当我选择第一个问题的答案并且如果我向下滚动并选择我的第二个问题答案时,我的第一个问题选择的答案再次自动取消选中
  • 您是否更改了所有其他输入字段的名称?如果输入字段的名称与我在上面发布的名称相同,那么是的,它将被取消选中。也许发布你的整个 html 以便我看一下?
  • 我已经更新了。实际上我不知道如何更改每个选项的值。我还需要最后打印选定的答案单选按钮或/选定的复选框值..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2020-03-16
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多