jQuery插入表行,然后在其中删除一个类

时间:2018-11-13 11:14:12

标签: javascript jquery

我有这个功能:

 //component.ts
 import { ValidatePWD } from './compare.validator';

 this.form = this._fb.group({
                  'user': ['', Validators.compose([Validators.required])],                       
                  'password': ['', Validators.compose([Validators.required])],
                  'verifypassword': ['', [Validators.required, ValidatePWD]],

 });


//compare.validator.ts
import { AbstractControl, FormGroup } from '@angular/forms';
export function ValidatePWD(control: AbstractControl ) {    
  if (control.value != "1234") {
    return { validPWD: true };
  }
  return null;
}


<div class="form-group">
      <label>Password: {{model.password}}</label>
      <input [(ngModel)]="model.password" [formControl]="password" class="form-control" type="password">
</div>

<div class="form-group">
     <label >Verify Password</label>
     <input[(ngModel)]="model.verifypassword" [formControl]="verifypassword"  class="form-control" type="password">
</div>

这是桌子:

function addAndRemoveClass() {

    $('.mytable tr:last').after('<tr class="someClass"><td>Inserted Row Data</td></tr>');

    $(".mytable tr").removeClass('.someClass');

}

我遇到的问题是由于某种原因,它没有删除先前注入到表中的类。

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

您无需在const tasks = [ fetch('https://jsonplaceholder.typicode.com/todos/1') , fetch('https://jsonplaceholder.typicode.com/todos/2') , fetch('https://jsonplaceholder.typicode.com/todos/3') ]; tasks.reduce((promiseChain, currentTask) => { console.log(promiseChain); return promiseChain.then(chainResults => { return currentTask.then(currentResult => [ ...chainResults, currentResult ] ) }); }, Promise.resolve([])).then(arrayOfResults => { // Do something with all results console.log(arrayOfResults); }); 函数中将.添加到

removeClass()

更改为

$(".mytable tr").removeClass('.someClass');

答案 1 :(得分:0)

removeClass('.someClass')替换为removeClass('someClass')

function addAndRemoveClass() {

  $('.mytable tr:last').after('<tr class="someClass"><td>Inserted Row Data</td></tr>');
  $(".mytable tr").removeClass('someClass');
}

addAndRemoveClass()
.someClass {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="mytable">
  <thead>
    <tr>
      <th>Format</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Something here</td>
    </tr>
  </tbody>
</table>