作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如何在使用 jquery 掩码时用 '*' 替换 ssn 的初始数字
例如:123-45-6789将作为 ***-**-6789 插入,但是当我们获取值时,我们应该得到 123456789
最佳答案
给定 HTML 如下:
<form id="ssn_form">
<input name="ssn" id="ssn" type="text">
<button id="submit">Submit</button>
</form>
您可以将 SSN 存储在模糊的数据属性中并屏蔽该值。然后在表单提交上,您可以在提交表单之前将值重新填充到输入中:
var retrieveValue = function(ev){
var $this = $(this),
val = $this.data('value');
if (val) {
$this.val(val);
}
},
hideValue = function(ev){
var $this = $(this);
$this.data('value', $this.val());
$this.val($this.val().replace(/^\d{5}/, '*****'));
};
$('#ssn').focus(retrieveValue);
$('#ssn').blur(hideValue);
$('#ssn_form').submit(function(ev){
ev.preventDefault();
retrieveValue.call($('#ssn')[0], ev);
$(this).submit();
});
关于jquery - 如何在使用 jquery 掩码时用 '*' 替换 ssn 的初始数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132397/
我是一名优秀的程序员,十分优秀!