问题是选中的复选框返回 true,而不是 input(type="checkbox") 的值。
Array or responded checked or not checked items
我无法将其存储在数组中(因为我需要)。另外,我无法准确指定 typescript 上的每个复选框名称,因为数据是动态的:
<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>
或者,如果无法获取这样的值,如何将“true”值的键保存在数组中:
最佳答案
像这样的事情应该可以解决问题:
const obj = {
a: true,
b: '',
c: true
};
const arr = Object.keys(obj).filter(x => obj[x])
console.log(arr) // --> ['a', 'c']
关于javascript - 在提交时将选中的复选框的值保存在数组中 Ionic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51534225/