最近由于需要我们将相关id的重复的去掉,一个是客户端一个后台程序把关,这里分享下js的去重复id的实现代码
<!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=gb2312" /> <title>js去重复id</title> </head> <body> <script type="text/javascript"> String.prototype.repeatOpt = function () { var str = this + "",objStr = ""; for (var i = 0; i < this.length; i++) { var s = str[i]; var newStr = str.replace(s, ''); var j = newStr.indexOf(s); if (j == -1) { objStr += s+","; } } return objStr; } alert("1,2,2,3,4,4,5,6".repeatOpt()); </script> </body> </html>
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
我经常用的代码:
<!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=gb2312" /> <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(0)) != -1) { var j=0, i = str.length; while (j < i && whitespace.indexOf(str.charAt(j)) != -1){ 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-1)) != -1){ var i = str.length - 1; while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1){ i--; } str = str.substring(0, i+1); } return str; } function doxgid() { document.form1.likeid.value = trim(document.form1.likeid.value.replace(new RegExp(',',"gm"),',')); document.form1.likeid.value = trim(document.form1.likeid.value.replace(new RegExp(' ',"gm"),',')); xgidcheck(); } function xgidcheck(){ if(document.form1.likeid.value!=""){ var arr1 = unique(document.form1.likeid.value.split(",")); document.form1.likeid.value=arr1.join(","); } } //去重复数组 function unique(data){ data = data || []; var a = {}; len = data.length; for (var i=0; i<len;i++){ var v = data[i]; if (typeof(a[v]) == 'undefined'){ a[v] = 1; } }; data.length=0; for (var i in a){ data[data.length] = i; } return data; } //专题增强 function doxgid2(theitem) { var theform=$Obj(theitem); theform.value = trim(theform.value.replace(new RegExp(',',"gm"),',')); theform.value = trim(theform.value.replace(new RegExp(' ',"gm"),',')); xgidcheck2(theform); } function xgidcheck2(theform){ if(theform.value!=""){ var arr1 = unique(theform.value.split(",")); theform.value=arr1.join(","); } } </script> </head> <body> <form name="form1"> 实例一: 单独的一个表单验证 <input name="likeid" type="text" id="likeid" size="60" onBlur="doxgid()" value="1,2,3,4,5,6,4,5,6"> 实例二:多个表单验证 <textarea name="jb51id" rows="3" id="jb51id" style="width:90%" onBlur="doxgid2('jb51id')">1,2,3,4,5,6,4,5,6</textarea> 先将鼠标定位在表单里面,然后离开就可以触发了。 onBlur </form> </body> </html>
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]