【发布时间】:2013-11-15 14:54:45
【问题描述】:
我发现这个脚本可以隐藏 SSN 的数字。我也想通过切换按钮来完成模糊/焦点事件的作用(并且按钮上的文本也会改变)。我试图通过 Click 事件来做到这一点,但需要帮助。有人可以看看小提琴。
我还希望 HIDDEN 状态显示 SSN 的最后 4 位,而 VISIBLE 状态显示所有数字。
HTML
<form>
<input name="ssn" id="ssn" type="text">
<button id="submit">Show SSN</button>
jQuery
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);
$("#submit").click(function () {
// check the visibility of the next element in the DOM
if ($(this).text("Hide SSN")) {
$('#ssn').val(retrieveValue);
} else {
$('#ssn').val(hideValue);
$('#submit').text("Show SSN");
}
});
点击[这里] (http://jsfiddle.net/squirc77/wEwYz/)
【问题讨论】: