js去除重复字符串两种实现⽅法复制代码代码如下:
function checkRe(checkid) {
var id = Qry("ctrid") + "_";
var values = window.parent.ElementById(id + "txt_dept_name").value; var keys = window.parent.ElementById(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 = place(new RegExp("," + newsValues), "");
keys = place(new RegExp("," + newsKeys), "");
}
values = okd(values);
keys = okd(keys);
window.parent.ElementById(id + "txt_dept_name").value = values; window.parent.ElementById(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 = String();
//去掉第⼀个逗号
if (s.substr(0, 1) == ',') {
s = s.substr(1);
}
return s;
}
function add_all() {
var ck = ElementsByTagName("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 = Qry("ctrid") + "_";
window.parent.ElementById(id + "txt_dept_name").value = ""; window.parent.ElementById(id + "hf_ids").value = "";
var ck = ElementsByTagName("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 == "" || String().match(new RegExp(ar2[i],"g")) == null)&&ar2[i]!=""){ array[j] =ar2[i];
array.sort();
j++;
}
}
String();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。