网站首页
cms
asp
php
jsp
asp.net
数据库
web前端
服务器
javascript
c#
vb.net
java
linux
office
数据结构
其他语言
网络安全
电脑故障
javascript
位置:
IT落伍者
>>
javascript
>> 浏览文章
js字符串去重复id的实现代码
发布日期:2019年08月25日
最近由于需要我们将相关id的重复的去掉
一个是客户端一个后台程序把关
这里分享下js的去重复id的实现代码
<!DOCTYPE html PUBLIC "
//W
C//DTD XHTML
Transitional//EN" "://www
w
org/
/xhtml"> <head> <meta http
equiv="Content
Type" content="text/html; charset=gb
" /> <title>js去重复id</title> </head> <body> <script type="text/javascript"> String
prototype
repeatOpt = function () { var str = this + ""
objStr = ""; for (var i =
; i < this
length; i++) { var s = str[i]; var newStr = str
replace(s
); var j = newStr
indexOf(s); if (j ==
) { objStr += s+"
"; } } return objStr; } alert("
"
repeatOpt()); </script> </body> </html>
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
我经常用的代码
<!DOCTYPE html PUBLIC "
//W
C//DTD XHTML
Transitional//EN" "://www
w
org/
/xhtml"> <head> <meta http
equiv="Content
Type" content="text/html; charset=gb
" /> <title>js去除重复id</title> <script type="text/javascript"> function $Obj(objname){ return document
getElementById(objname); } //供使用者调用 function trim(s){ return trimRight(trimLeft(s)); } //去掉左边的空白 function trimLeft(s){ if(s == null) { return ""; } var whitespace = new String(" tnr"); var str = new String(s); if (whitespace
indexOf(str
charAt(
)) !=
) { var j=
i = str
length; while (j < i && whitespace
indexOf(str
charAt(j)) !=
){ j++; } str = str
substring(j
i); } return str; } //去掉右边的空白 function trimRight(s){ if(s == null) return ""; var whitespace = new String(" tnr"); var str = new String(s); if (whitespace
indexOf(str
charAt(str
length
)) !=
){ var i = str
length
; while (i >=
&& whitespace
indexOf(str
charAt(i)) !=
){ i
; } str = str
substring(
i+
); } return str; } function doxgid() { document
form
likeid
value = trim(document
form
likeid
value
replace(new RegExp(
"gm")
)); document
form
likeid
value = trim(document
form
likeid
value
replace(new RegExp(
"gm")
)); xgidcheck(); } function xgidcheck(){ if(document
form
likeid
value!=""){ var arr
= unique(document
form
likeid
value
split("
")); document
form
likeid
value=arr
join("
"); } } //去重复数组 function unique(data){ data = data || []; var a = {}; len = data
length; for (var i=
; i<len;i++){ var v = data[i]; if (typeof(a[v]) ==
undefined
){ a[v] =
; } }; data
length=
; for (var i in a){ data[data
length] = i; } return data; } //专题增强 function doxgid
(theitem) { var theform=$Obj(theitem); theform
value = trim(theform
value
replace(new RegExp(
"gm")
)); theform
value = trim(theform
value
replace(new RegExp(
"gm")
)); xgidcheck
(theform); } function xgidcheck
(theform){ if(theform
value!=""){ var arr
= unique(theform
value
split("
")); theform
value=arr
join("
"); } } </script> </head> <body> <form name="form
"> 实例一
单独的一个表单验证 <input name="likeid" type="text" id="likeid" size="
" onBlur="doxgid()" value="
"> 实例二
多个表单验证 <textarea name="jb
id" rows="
" id="jb
id" style="width:
%" onBlur="doxgid
(
jb
id
)">
</textarea> 先将鼠标定位在表单里面
然后离开就可以触发了
onBlur </form> </body> </html>
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
上一篇:
公共js在页面底部加载的注意事项介绍
下一篇:
jQuery插件编写