日期:2014-05-17 浏览次数:20939 次
<body> <script language="javascript"> function test() { var v1 = ""; var v2 = ""; var reRecords = new Array(); var LEN = document.getElementById("qty").value; //文本框的个数 //***************************************** for(var i=1; i<=LEN ; i++) { if(document.getElementById("weeknumber" + i).value=="") { alert("周别不能为空!"); document.getElementById("weeknumber" + i).focus(); return false; } } //***************************************** //通过两层 for 循环得到相同的元素,并加入到 reRecords 数组中 for(var i=1; i<=LEN ; i++) { if(document.getElementById("weeknumber" + i) != null) { v1 = document.getElementById("weeknumber" + i).value; } for(var j=i+1; j<=LEN ; j++) { if(document.getElementById("weeknumber" + j) != null) { v2 = document.getElementById("weeknumber" + j).value; if(v1 == v2) { //alert("v2 = " + v2); reRecords[reRecords.length] = v2; break; } } } } //取得相同元素结束 //去掉 reRecords 数组中相同的元素,即:将含有相同元素的值用""来代替 for(var i=0; i<reRecords.length; i++) { var s1 = reRecords[i]; if(s1 == "") { break; } for(var j=i+1; j<reRecords.length; j++) { var s2 = reRecords[j]; if(s1 == s2) { reRecords[j] = ""; } } } //用 "" 代替元素结束 //数组中剩下的非 "" 元素就是最后想要的值,即:构造str的过程。 var str = "\r\n"; for(var i=0; i<reRecords.length; i++) { if(reRecords[i] != "") { str += reRecords[i]; str += "\r\n"; alert("發現重复的值是:" + str);str = ""; //重置 str 为空 return false; } } //构造str 结束 alert('保存成功!'); for(var i=1; i<=LEN ; i++) { document.getElementById("weeknumber" + i).value=""; } } </script> <form id="form1" name="form1" method="post" action=""> <% for i=1 to 5%> <input type="text" name="weeknumber<%=i%>" id="weeknumber<%=i%>"/><br /> <% next %> <input name="qty" id="qty" type="hidden" value="<%= i-1%>" /> <input type="button" name="button" id="button" value="提交" onclick="test();" /> </form> </body>