【问题标题】:Save the value of checked checkboxes in an array on submit Ionic在提交 Ionic 时将选中复选框的值保存在数组中
【发布时间】:2018-07-26 08:23:02
【问题描述】:

问题是选中的复选框返回 true,而不是 input(type="checkbox") 的值。

Array or responded checked or not checked items

而且我不能将它存储在数组中(因为我需要)。此外,我无法准确指定打字稿上的每个复选框名称,因为数据是动态的:

<ion-col *ngFor="let resKeyFt of keyFeatures; let i = index" class="p-0" col-6>
      <ion-item>
             <label for="{{resKeyFt}}">{{valueFeatures[i]}}</label>
             <input id="{{resKeyFt}}" type="checkbox"
                    value="{{resKeyFt}}" name="{{resKeyFt}}" ngModel>
      </ion-item>
</ion-col>

或者如果无法获得这样的值,我如何将“真”值的键保存在数组中:

Stored Array

【问题讨论】:

  • 你需要绑定数组数据吗?for checkbox

标签: javascript angular typescript ionic-framework


【解决方案1】:

这样的事情应该可以解决问题:

const obj = {
    a: true,
    b: '',
    c: true
};

const arr = Object.keys(obj).filter(x => obj[x])
console.log(arr) // --> ['a', 'c']

【讨论】:

  • 这工作得很好,但是在数组中是“type=text”输入,它还返回它们的键,而不仅仅是“true”键。是否可以对其进行过滤并仅保存“真实”键?
  • 好的,把过滤器回调改成x =&gt; obj[x] === true
  • 很高兴它有帮助 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多