日期:2014-05-16 浏览次数:20354 次
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); } }); }
<!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>