日期:2014-05-16 浏览次数:20373 次
<html> <head> <script language="javascript" type="text/javascript"> var arr = []; var noCheckHTML = "< 未选择 >" function init() {//load初始化事件 with(document.getElementById("selectedPlayerID")) { innerHTML = noCheckHTML; } var chk =document.getElementsByName("type[]"); for(var i=0;i<chk.length;i++) { chk[i].checked = false; chk[i].onclick=new Function("chkClick(this.checked,this.title)") } } Array.prototype.contains = function(o) { //判断数组arr中是否含有元素o var flag = false; for(var i=0;i<this.length;i++) { if(this[i]==o) { flag = true; break; } } return flag; } function chkClick(_isChecked,_title) { _isChecked?arr.push(_title):arr.pop(_title); with(document.getElementById("selectedPlayerID")) { arr.length>0?innerHTML = "< "+arr.toString()+" >":innerHTML = noCheckHTML; } } </script> </head> <body onload="init()"> <form ACTION="" name="form1" > <table > <tr > <td > <input type="checkbox" name="type[]" value="1" title="类别一"> 类别一 <input type="checkbox" name="type[]" value="2" title="类别二"> 类别二 <input type="checkbox" name="type[]" value="3" title="类别三"> 类别三 <td > </tr > <tr > <td > <span id="selectedPlayerID" >< 未选择 > </span > <td > </tr > </table > </form > </body> </html>
------解决方案--------------------
<html> <head> <script language="javascript" type="text/javascript"> var arr = []; var noCheckHTML = "< 未选择 >" function init() {//load初始化事件 with(document.getElementById("selectedPlayerID")) { innerHTML = noCheckHTML; } var chk =document.getElementsByName("type[]"); for(var i=0;i<chk.length;i++) { chk[i].checked = false; chk[i].onclick=new Function("chkClick(this.checked,this.title)") } } Array.prototype.contains = function(o) { //判断数组arr中是否含有元素,有返回index,没有返回-1 var index = -1; for(var i=0;i<this.length;i++) { if(this[i]==o) { index = i; break; } } return index; } Array.prototype.remove = function(o) { //判断数组arr中是否含有元素o var index; var t = this; if(