你指的顺序是 按input出现的次序还是 id的顺序如果是出现次序就简单点var inputCollection = new Array();
var inputs = [form].getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if((s=inputs[i]).type=="text" && s.name.replace("a","b")==s.id)
inputCollection.push(s);
}
var inputs = [form].getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if((s=inputs[i]).type=="text" && s.name.replace("a","b")==s.id)
inputCollection.push(s);
}
for(var i=0;i<a.length;i++){
if(a[i].type=="text"){
if(a[i].name.substr(0,1)=="a"){
alert(a[i].value)
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="javascript" />
<meta name="description" content="for javascript region of csdn" />
</head> <body>
<input type="text" name="a1" id="b1">
<input type="text" name="a2" id="b2">
<input type="text" name="a3" id="b3">
<input type="text" name="a4" id="b4">
<input type="button" id="btnShow" value="Show" onclick="showTextBoxes();" />
<script type="text/javascript">
<!--
function showTextBoxes()
{
var colInput = document.getElementsByTagName("input");
for (var i=0; i<colInput.length; i++)
{
if (colInput[i].type=="text")
{
if (colInput[i].value=="")
{
alert(colInput[i].id + " is empty.")
}
else
{
alert(colInput[i].id + " is not empty.\nThe value is " + colInput[i].value)
}
}
}
}
//-->
</script>
</body>
</html>
var arr = new Array();
var inputs = [form].getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
if((s=inputs[i]).type=="text" && s.name.replace("a","b")==s.id)
arr[arr.length]=Number(s.name.replace("a"));arr.sort();
for(var i=0;i<arr.length;i++)
inputCollection.push(document.getElementById("a" + arr[i]));
inputCollection就是集合
如果不知道可以用 document代替
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="javascript" />
<meta name="description" content="for javascript region of csdn" />
</head> <body>
<input type="text" name="a1" id="b1">
<input type="text" name="a2" id="b2">
<input type="text" name="a3" id="b3">
<input type="text" name="a4" id="b4">
<input type="button" id="btnShow" value="Show" onclick="showTextBoxes();" />
<script type="text/javascript">
<!--
function showTextBoxes()
{
var colInput = document.getElementsByTagName("input");
var textCollection = new Array();
for (var i=0; i<colInput.length; i++)
{
if (colInput[i].type=="text")
{
if (colInput[i].value=="")
{
alert(colInput[i].id + " is empty.")
}
else
{
textCollection[textCollection.length] = colInput[i].id + " | " + colInput[i].value;
}
}
}
alert(textCollection);
}
//-->
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="javascript" />
<meta name="description" content="for javascript region of csdn" />
</head> <body>
<input type="text" name="a1" id="b1">
<input type="text" name="a2" id="b2">
<input type="text" name="a3" id="b3">
<input type="text" name="a4" id="b4">
<input type="button" id="btnShow" value="Show" onclick="showTextBoxes();" /> <script type="text/javascript">
<!--
function showTextBoxes()
{
var colInput = document.getElementsByTagName("input");
var textCollection = new Array();
for (var i=0; i<colInput.length; i++)
{
if (colInput[i].type=="text")
{
if (colInput[i].value=="")
{
alert(colInput[i].id + " is empty.")
}
else
{
textCollection[textCollection.length] = colInput[i].id + " | " + colInput[i].value;
}
}
}
if (textCollection.length > 0)
{
alert(textCollection); var oNewInput = document.createElement("input");
oNewInput.type = "text";
oNewInput.value = textCollection;
document.body.appendChild(oNewInput);
}
}
//-->
</script>
</body>
</html>
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if((s=inputs[i]).type=="text")
{
if(v=s.value.replace(/(^\s+)|(\s+$)/g,"").length>0)
textCollection.push(s.value);
else
alert(s.id + ": 为空");
}
}
alert(textCollection);
这样动态生成那判定就没有意义了-。-
我设想的是得到最终的textCollection,就是数据填完的textCollection-.-,这样填一个数据点show也会出textCollection。。-。-再帮帮忙,改下下吧^^
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="javascript" />
<meta name="description" content="for javascript region of csdn" />
</head> <body>
<div id="divContainer">
<input type="text" name="a1" id="b1">
<input type="text" name="a2" id="b2">
<input type="text" name="a3" id="b3">
<input type="text" name="a4" id="b4">
</div>
<input type="button" id="btnShow" value="Show" onclick="showTextBoxes();" /><br />
<input type="text" id="tbxResult"> <script type="text/javascript">
<!--
function showTextBoxes()
{
var colInput = document.getElementById("divContainer").getElementsByTagName("input");
var textCollection = new Array();
for (var i=0; i<colInput.length; i++)
{
if (colInput[i].type=="text")
{
if (colInput[i].value=="")
{
alert(colInput[i].id + " is empty.")
}
else
{
textCollection[textCollection.length] = colInput[i].id + " | " + colInput[i].value;
}
}
}
if (textCollection.length > 0)
{
alert(textCollection);
document.getElementById("tbxResult").value = textCollection;
}
}
//-->
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="[email protected]" />
<meta name="keywords" content="javascript" />
<meta name="description" content="for javascript region of csdn" />
</head> <body>
<div id="divContainer">
<input type="text" name="a1" id="b1">
<input type="text" name="a2" id="b2">
<input type="text" name="a3" id="b3">
<input type="text" name="a4" id="b4">
</div>
<input type="button" id="btnShow" value="Show" onclick="return showTextBoxes();" /><br />
<input type="text" id="tbxResult"> <script type="text/javascript">
<!--
function showTextBoxes()
{
var colInput = document.getElementById("divContainer").getElementsByTagName("input");
var textCollection = new Array();
for (var i=0; i<colInput.length; i++)
{
if (colInput[i].type=="text")
{
if (colInput[i].value=="")
{
alert(colInput[i].id + " is empty.")
return false;
}
else
{
textCollection[textCollection.length] = colInput[i].id + " | " + colInput[i].value;
}
}
}
if (textCollection.length > 0)
{
alert(textCollection);
document.getElementById("tbxResult").value = textCollection;
}
}
//-->
</script>
</body>
</html>