使用 jQuery 替换字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3661415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:51:44  来源:igfitidea点击:

Replacing characters using jQuery

jquery

提问by Abe Miessler

I'm trying to remove commas from all of my textboxes on keyup. I came up with the script below but it's not working. Can anyone see what I am doing wrong?

我正在尝试从 keyup 上的所有文本框中删除逗号。我想出了下面的脚本,但它不起作用。谁能看到我做错了什么?

<script>
    $("input[type='text']").keyup
    (
        function () 
        {
            alert('1');
            $(this).val($(this).val().replace(/[,]/g, ""));
        }
    );
</script>

NOTE:please excuse the $ in Script. SO won't let me post it otherwise...

注意:请原谅脚本中的 $。所以不会让我发布它否则...

回答by jessegavin

You might want to wrap that whole chunk of code in a document ready function

您可能希望将整个代码块包装在文档就绪函数中

$(function() {
  $("input:text").keyup(function() {
        $(this).val($(this).val().replace(/[,]/g, ""));
  });
});

You can read all about this on the jQuery documentation site.

您可以在jQuery 文档站点上阅读所有相关内容。

回答by Andy E

As others have mentioned, make sure you're using $(document).ready() - http://api.jquery.com/ready/. Also, instead of replacing commas on keyup, you should disallow them on keypress by returning false:

正如其他人所提到的,请确保您使用的是 $(document).ready() - http://api.jquery.com/ready/。此外,不要在 keyup 上替换逗号,而应通过返回 false 来禁止在 keypress 上使用逗号:

$(document.ready(function () { 
    $("input[type=text]").keypress(function (evt) {
        if (String.fromCharCode(evt.which) == ",")
            return false;
    });
});

Example: http://jsfiddle.net/QshDd/

示例:http: //jsfiddle.net/QshDd/

This gives a more professional feel, the "," is blocked without appearing and then disappearing when you release the key. Like your solution, however, this won't catch copying and pasting commas into your input. For that, you can hook into the onpasteor onchangeevent.

这给人一种更专业的感觉,“,”被挡住了,不会出现,然后在您松开按键时消失。但是,就像您的解决方案一样,这不会将逗号复制和粘贴到您的输入中。为此,您可以挂钩onpasteonchange事件。

If you want to stick with keyup and replace, you don't really need to mess around with jQuery wrappings, you can access the valueproperty directly:

如果你想坚持使用 keyup 和替换,你真的不需要搞乱 jQuery 包装,你可以直接访问value属性:

$(document.ready(function () { 
    $("input[type=text]").keyup(function (evt) {
        this.value = this.value.replace(/,/g, "");
    });
});

回答by aularon

回答by bevacqua

<script>
    $(document).ready(function()
    {
        $("input[type='text']").live('keyup',function () 
        {
            alert('1');
            $(this).val($(this).val().replace(/[,]/g, ""));
        });
    });
</script>

If your input is in an update panel, or added after the binding takes place, this should work.

如果您的输入在更新面板中,或者在绑定发生后添加,这应该可以工作。

回答by Maulik Vora

Try this

尝试这个

<script>
        $("input[type='text']").keyup
        (
            function () 
            {
                alert('1');
                $(this).val($(this).val().replace(',', ""));
            }
        );
    </script>