我想在不使用change()
或click()
函数的情况下获取组件中表单的所有已检查项目,因为它无法获取已检查的项目。
这是我在TS中的数组:
PartyRoles = [
{
Id: 1,
Name: "Vendor",
Checked: true
},
{
Id: 2,
Name: "Client",
Checked: true
},
{
Id: 3,
Name: "Consigner",
Checked: false
}
]
我的HTML表单:
<form (ngSubmit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles">
<label>
<input type="checkbox" value="{{item.Id}}" [attr.checked]="item.Checked==true ? true : null" [attr.disabled]="item.Id==1 ? true : null" />
<span innerHTML="{{item.Name}}"></span>
</label>
</div>
</form>
我的onSubmit函数,我想在其中获取所有选中的值:
editPartyRolesSubmit= function () {
// Please suggest how to fetch checked items
}
答案 0 :(得分:8)
您可以使用Form和NgModel
<form #checkboxForm="ngForm" (submit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles">
<label>
<input type="checkbox" value="{{item.Id}}" [(ngModel)]="item.Checked" [name]="item.Name" [attr.disabled]="item.Id==1 ? true : null" />
<span innerHTML="{{item.Name}}"></span>
</label>
</div>
<input type="submit" value="Submit">
</form>
功能可以
editPartyRolesSubmit() {
console.log(this.PartyRoles);
}
答案 1 :(得分:1)
将您的Html更改为以下内容:
<form (ngSubmit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles">
<label>
<input type="checkbox" value="{{item.Id}}"
[checked]="item.Checked"
(change)="item.Checked = !item.Checked"
[attr.disabled]="item.Id==1 ? true : null" />
<span innerHTML="{{item.Name}}"></span>
</label>
</div>
</form>
您的PartyRoles
将保持更新最新的复选框值。您可以像这样访问它:
editPartyRolesSubmit(){
// Access each item like this in PartyRoles
this.PartyRoles.forEach(role=>{
console.log('Id: ' + role.Id + ', Name: ' + role.Name + ', Checked: ' + role.Checked);
});
// Or like this
let role = this.PartyRoles.find(x=>x.Id === 1);
if(role !== undefined){
console.log(role);
}
// Or a filetered list
let checkedRoles = this.PartyRoles.filter(x=>x.Checked === true);
console.log(checkedRoles);
// Or by array index
let roleTwo = this.PartyRoles[2];
console.log(roleTwo);
}
您不必在方法中使用function
关键字。这是指向working demo的链接。
答案 2 :(得分:1)
您可以使用:
<form (ngSubmit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles; let i = index">
<label>
<input type="checkbox"
[attr.checked]="item.Checked == true ? true : null"
(change)="item.Checked = !item.Checked"
[attr.disabled]="item.Id==1 ? true : null" />
<span innerHTML="{{item.Name}}"></span>
</label>
</div>
<button type="submit">Click</button>
</form>
或者:
<form (submit)="editPartyRolesSubmit()">
<div *ngFor="let item of PartyRoles">
<label>
<input type="checkbox" value="{{item.Id}}" [(ngModel)]="item.Checked" [name]="item.Name" [attr.disabled]="item.Id==1 ? true : null" />
<span innerHTML="{{item.Name}}"></span>
</label>
</div>
<button type="submit">Click</button>
</form>
答案 3 :(得分:0)
另一种方式,
在你的 html 中
<li *ngFor="let course of courses.ugreg">
<mat-checkbox [checked]="course.checked" [(ngModel)]="course.checked" name="course.name">
{{course.name}}
</mat-checkbox>
</li>
在你的打字稿中
selectedcourses = [];
courses: Courses = {
name: 'Courses',
checked: false ,
ugreg: [
{ name: 'B Com(Computer)*',checked: false},
{ name: 'B Com(Marketing)*', checked: false},
{ name: 'BBA*', checked: false },
{ name: 'BCA**', checked: false }
],
};
并在您的表单中提交按钮方法
onSubmit() {
this.courses.ugreg.forEach(x => { if (x.checked) { this.selectedcourses.push(x.name); }
})
答案 4 :(得分:-1)
您可以为所选项目创建单独的数组
SelectedList=[]
for(let i=0;i<PartyRoles.length;i++){
if(PartyRoles[i].checked){
SelectedList.push(PartyRoles[i]);
}
}