- JScript code
<HTML> <HEAD> <TITLE> test </TITLE> </HEAD> <BODY> <script language="javascript"> function getNumber() { alert(); } </script> </BODY> <form name = "myForm"> <input type = "button" name = "myButton" value = "1"> <input type = "button" name = "myButton" value = "2"> <input type = "button" name = "myButton" value = "3"> </form> </HTML>
请问各位大侠如何用js弹出每个input的值啊?
------解决方案--------------------
首先你的所有button不要用同一个名称(name)---建议
获取方法:
- JScript code
function getNumber(){ var t = document.getElementsByTagName("input"); for(var i=0; i<t.length; i++){ alert(t[i].value); } }
------解决方案--------------------
- HTML code
<HTML> <HEAD> <TITLE> test </TITLE> <script language="javascript"> function showSingleInputValue(n){ alert('第三个Input的值为:'+document.getElementsByTagName("myButton")[n].value); } function showAllInputValue(){ var o = document.getElementsByTagName("INPUT");//取得所有Input对象集合; var arr=[];//定义一个数组用来存放Input对象值 for(var i=0; i<o.length; i++){ arr.push(o[i].value);//将各个Input对象值压入数组 } document.getElementsByName("myText")[0].value=arr.jion(",") alert('共有'+o.length+'个Input对象,其值分别为:'+arr); } window.onload=function(){ showAllInputValue()//页面加载完毕后显示所有Input对象的值 } </script> </HEAD> <BODY> <form name = "myForm"> <input type = "text" name = "myText" value = "0"> <input type = "button" name = "myButton" value = "1"> <input type = "button" name = "myButton" value = "2"> <input type = "button" name = "myButton" value = "3"> </form> <input type = "button" name = "showSingleButtonValue" value = "显示第三个Input对象的值" onclick="showSingleInputValue(2)" /> </BODY> </HTML>