首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

去掉重复的关键字,该如何处理

2012-05-24 
去掉重复的关键字input namesea typetext /JScript codefunction Sea(){$(input[namesea]).b

去掉重复的关键字

<input name="sea" type="text" />

JScript code
function Sea(){    $("input[name='sea']").bind('keydown', function(e) {        if(e.which == 188) {            var seana = $(this).val(), reg = /(?:^|,)(.+?)(?=(?:,.+?)*,\1(?:,|$))/ig;            seana = seana.replace(reg, "");            seana = seana.replace(/^,/, "");            $(this).val(seana);        }    });}


去掉重复的关键字,

现在有个问题就是 如:关字键,关键字,字关键,,, 这个只要 关字键,关键字,字关键, 多了的“,”也自动去掉

[解决办法]
HTML code
<!DOCTYPE HTML><html>    <head>        <meta charset="gb2312" />        <title></title>        <style>                </style>    </head>    <body>        <input size="100" id="test" value="1,2,3,2,1,5,4,5,," />        <button id="btn">点击</button>        <script>            Array.prototype.unique = function() {                var temp = {},                len = this.length;                for (var i = 0; i < len; i++) {                    if (typeof temp[this[i]] == "undefined") {                        temp[this[i]] = 1;                    }                }                this.length = 0;                len = 0;                for (var i in temp) {                    this[len++] = i;                }                return this;            }                        var $ = function(id){                return document.getElementById(id);            };            $('btn').onclick = function(){                var arr = $('test').value.split(',');                var str = arr.unique().join(',');                var r = /^,|,$/;                $('test').value = str.replace(r, '');            }                    </script>    </body></html> 

热点排行
Bad Request.