日期:2014-05-16 浏览次数:20363 次
<input id="btn_tname" name="" type="text" value="" /> <input id="btn_submit" name="" type="button" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> var ar = []; window.onload = function() { document.getElementById('btn_submit').onclick = function() { var v = document.getElementById('btn_tname').value; if (v.length > 0) { var FLAG = false; //是否已存在标志 for (var i = 0; i < ar.length; i ++) { if (ar[i] == v) { FLAG = true; break; } } if (!FLAG) ar.push(v); } alert(ar); } } </script> </head> <body> <input id="btn_tname" name="" type="text" value="" /> <input id="btn_submit" name="" type="button" value="添加到数组" /> </body> </html>
------解决方案--------------------
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> </head> <body> <input id="btn_tname" name="" type="text" value="" /> <input id="btn_submit" name="" type="button" /> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } Array.prototype.find = function(val){ for(var i = 0, len = this.length; i < len; i++){ if( this[i] === val ){ return i; } } return -1; } var arr = []; $('btn_submit').onclick = function(){ var s = $('btn_tname').value; if( s != '' && arr.find(s) == -1 ){ arr.push(s); } alert(arr) } </script> </body> </html>
------解决方案--------------------
$(function(){ var arr=new Array(); $("#btn_tname").click(function(){ var val=$(this).val(); for(var i=0;i<arr.length;i++) { if(arr[i]==val) break; } arr.push(val); }) })