我如何将其转换jQuery code
为plain JavaScript
?
$("#txtTestNumbersOnlyRegex").keyup(function () {
var newValue = $(this).val().replace(/[^0-9]/g,'');
$(this).val(newValue);
});
简单如下
document.querySelector('#txtTestNumbersOnlyRegex').onkeyup = function() {
var newValue = this.value.replace(/[^0-9]/g, '');
this.value = newValue;
}
更新:使用 Angular
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, HelloComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
// app.component.html
<input type="text" [(ngModel)]="value" (keyup)="replacer(value)">
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 4';
text: string;
replacer(value: string) {
this.text = value.replace(/[^0-9]/g, '');
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句