DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js去除重復字符串兩種實現方法
js去除重復字符串兩種實現方法
編輯:關於JavaScript     
方法一:
復制代碼 代碼如下:
function checkRe(checkid) {

var id = window.parent.getQry("ctrid") + "_";

var values = window.parent.opener.document.getElementById(id + "txt_dept_name").value;
var keys = window.parent.opener.document.getElementById(id + "hf_ids").value;

var ary = checkid.value.split("|");
var newsValues = ary[0];
var newsKeys = ary[1];

if (checkid.checked) {
values = values + "," + newsValues;
keys = keys + "," + newsKeys;

}
else {
values = values.replace(new RegExp("," + newsValues), "");
keys = values.replace(new RegExp("," + newsKeys), "");

}

values = okd(values);
keys = okd(keys);

window.parent.opener.document.getElementById(id + "txt_dept_name").value = values;
window.parent.opener.document.getElementById(id + "hf_ids").value = keys;

return;

}
//轉換成數組,去掉重復,再組合好。
function okd(str) {

var ary = str.split(",");
// 去重的算法
var json = {};
for (var i = 0; i < ary.length; i++) {

if (ary[i] != "") {

json["a" + ary[i]] = ary[i];
}
}

// 查看結果
var str2 = "";
for (var key in json) {
str2 += "," + json[key];
}

var s = str2.toString();

//去掉第一個逗號
if (s.substr(0, 1) == ',') {
s = s.substr(1);
}

return s;
}

function add_all() {
var ck = document.getElementsByTagName("input");
for (i = 0; i < ck.length; i++) {
if (ck[i].type == "checkbox") {
ck[i].checked = true;
checkRe(ck[i]);

}
}

}

function del_all() {
var id = window.parent.getQry("ctrid") + "_";
window.parent.opener.document.getElementById(id + "txt_dept_name").value = "";
window.parent.opener.document.getElementById(id + "hf_ids").value = "";

var ck = document.getElementsByTagName("input");
for (i = 0; i < ck.length; i++) {
if (ck[i].type == "checkbox") {
ck[i].checked = false;

}
}
}

復制代碼 代碼如下:
<input id='chk_pkId' type='checkbox' name='chk' onclick='checkRe(this);' value='值|pkId' />

方法二:
復制代碼 代碼如下:
function filterRepeatStr(str){
var ar2 = str.split(",");
var array = new Array();
var j=0
for(var i=0;i<ar2.length;i++){
if((array == "" || array.toString().match(new RegExp(ar2[i],"g")) == null)&&ar2[i]!=""){
array[j] =ar2[i];
array.sort();
j++;
}
}
return array.toString();
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved