jQuery将百分号添加到输入框 [英] Jquery add percent sign to input box

查看:697
本文介绍了jQuery将百分号添加到输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个输入框,当输入数字时,该输入框会自动向用户添加一个可见的百分号(不仅仅是在提交时将其识别为百分号).因此,用户点击"2"并看到"2%"

I'd like to have an input box that automatically adds a visible percent sign to the user when entering numbers (not just recognizes it as a percent when submitting). Thus, a user hits "2" and sees "2%"

我假设一个人可以使用Jquery相当容易地做到这一点,但是我不知道怎么做!有什么想法吗?

I'm assuming one could use Jquery to do this fairly easily, but I have no idea how! Any ideas?

谢谢大家.

推荐答案

您可以处理change事件:

$(':input.Percent').change(function() {
    $(this).val(function(index, old) { return old.replace(/[^0-9]/g, '') + '%'; });
});

这篇关于jQuery将百分号添加到输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆