6

我想在不使用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
  }
4

5 回答 5

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);
}
于 2017-08-31T07:46:09.673 回答
2

你可以使用这个:

<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>
于 2017-08-31T08:15:41.100 回答
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方法中使用关键字。这是工作演示的链接。

于 2017-08-31T07:41:04.750 回答
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); } 
 })
于 2021-03-10T15:58:33.263 回答
-1

您可以为选定的项目创建一个单独的数组

let SelectedList=[]
for(let i=0;i<PartyRoles.length;i++){
 if(PartyRoles[i].checked){
  SelectedList.push(PartyRoles[i]);
 }
}
于 2017-08-31T07:32:20.993 回答