日期:2014-05-16  浏览次数:20441 次

怎么按顺序取一组文本框的值?急啊,大家帮帮忙
打个比方一组input框
<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框的值,然后组成个集合,还有判定这组框中有没有空值。。
这里的name何id是动态生成的,不知道这组input框的数量,关键是如何按顺序取得这组框的值?

------解决方案--------------------
var textCollection = new Array();
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);

------解决方案--------------------
看看行不,俺先去吃饭了,哈

<!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>
<title> new document </title>
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "yixianggao@126.com " />
<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>