日期:2014-05-16  浏览次数:20354 次

去掉重复的关键字

<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>