删除符号从文本字段与jquery

remove symbols from text field with jquery

本文关键字:jquery 字段 文本 符号 删除      更新时间:2023-09-26

我有从文本中删除符号的功能。如何保持逗号和点。,)在文本字段?因为目前函数删除了所有非数字

$('.numbersOnly').bind("keyup paste", function(){
        setTimeout(jQuery.proxy(function() {
            this.val(this.val().replace(/[^0-9]/g, ''));
        }, $(this)), 0);
});

试试这个

this.val(this.val().replace(/[^0-9,.]/g, ''));

试试下面的代码:-

$('.numbersOnly').bind("keyup paste", function(){
        setTimeout(jQuery.proxy(function() {
             this.val(this.val().replace(/[^0-9.,]/g, '')
        }, $(this)), 0);
});

更改为:

$('.numbersOnly').bind("keyup paste", function(){
        setTimeout(jQuery.proxy(function() {
            this.val(this.val().replace(/[^0-9'.',]/g, ''));
        }, $(this)), 0);
});