用百分号输入掩码
问题描述:
我想让用户输入1或2个数字,并显示数字后面跟百分号。如果没有输入数字,它应该是空白的。附件几乎可以工作,但如果只输入一位数字,则不会显示百分号。谁能帮忙?由于用百分号输入掩码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>Mask</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://cloud.github.com/downloads/digitalBush/jquery.maskedinput/jquery.maskedinput-1.3.js" type="text/javascript"></script>
<script>
$(function(){$(".percent").mask("9?9%");});
</script>
</head>
<body>
<input type="text" name="percent" class="percent" value="" />
</body>
</html>
答
试试这个 - http://jsfiddle.net/dm8Mb/12/
$(function(){
$(".percent").mask("9?9%");
$(".percent").on("blur", function() {
var value = $(this).val().length == 1 ? $(this).val() + '%' : $(this).val();
$(this).val(value);
})
});
感谢容易实现的解决方案。 – 2013-01-09 12:09:18
@RAJK欢迎您:) – 2013-01-09 13:01:05
这是非常低效的。你要包装(并重新评估)''($)这个分开的四次,然后执行三次val()函数。您应该将这些结果存储在临时变量中。在JavaScript中,变量很便宜,执行起来很昂贵。 – 2014-09-27 12:08:41