<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>filterStr.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript"> String.prototype.replaceAll = function(s1, s2) { var temp = this; while (temp.indexOf(s1) != -1) { temp = temp.replace(s1, s2); } return temp; }
function filter(o) { var temp = o.value; var index = temp.lastIndexOf('*'); index = index == -1 ? 0 : index + 1; var val = temp.substring(index, temp.length); var filterVal = o.getAttribute("filterValue") == undefined ? "" : o.getAttribute("filterValue"); o.setAttribute("filterValue", filterVal + val); var ary = temp.split(''); var result = ""; for (var i = 0; i < ary.length; i++) { if (ary[i] != "*") { result = temp.replaceAll(ary[i], '*'); } } o.value = result; }
function getVal() { alert(document.getElementById("s").getAttribute("filterValue")); } </script> </head>
<body> <input type="text" id="s" onkeyup="filter(this)"/> <input type="button" value=" get Value " onclick="getVal()"> </body> </html>
随时保存住原来的值。用于提交到后台
看到PLMM,就情不自禁吧,哈哈
if (.....) {
if (f.getValue().length < 10) {
window.numid = f.getValue();//window.numid保存原值,用于提交
window.flags = false;
} else {
if (!window.flags) {
window.numid = f.getValue();
}
(function() { f.setValue(f.getValue().substring(0, 10) + "******") }).defer(20);
window.flags = true;
}
} else {
window.numid = f.getValue();
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>filterStr.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
String.prototype.replaceAll = function(s1, s2) {
var temp = this;
while (temp.indexOf(s1) != -1) {
temp = temp.replace(s1, s2);
}
return temp;
}
function filter(o) {
var temp = o.value;
var index = temp.lastIndexOf('*');
index = index == -1 ? 0 : index + 1;
var val = temp.substring(index, temp.length);
var filterVal = o.getAttribute("filterValue") == undefined ? "" : o.getAttribute("filterValue");
o.setAttribute("filterValue", filterVal + val);
var ary = temp.split('');
var result = "";
for (var i = 0; i < ary.length; i++) {
if (ary[i] != "*") {
result = temp.replaceAll(ary[i], '*');
}
}
o.value = result;
}
function getVal() {
alert(document.getElementById("s").getAttribute("filterValue"));
}
</script> </head>
<body>
<input type="text" id="s" onkeyup="filter(this)"/>
<input type="button" value=" get Value " onclick="getVal()">
</body>
</html>
这个还是没有实现,通过 id.value 来取值,就是增加了一个属性来保存未格式化的值,还有这个方法还有需要改进的地方。另一个属性的保存也有问题.还有一个问题,如果我用 id.value = "anyValue" 后,我也要能将值格式化后,再显示.有没有什么好办法.