怎么按顺序取一组文本框的值?急啊,大家帮帮忙

a2bb 2007-08-13 08:48:25
打个比方一组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框的数量,关键是如何按顺序取得这组框的值?
...全文
542 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
a2bb 2007-08-13
  • 打赏
  • 举报
回复
恩,差不多就是这个效果啦。。谢谢谢谢。。非常感谢。。lol,SF拉~

<!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="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>
yixianggao 2007-08-13
  • 打赏
  • 举报
回复
看看行不,俺先去吃饭了,哈

<!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>
a2bb 2007-08-13
  • 打赏
  • 举报
回复
yixianggao(你我他,三人行必有我师焉!)
这样动态生成那判定就没有意义了-。-
我设想的是得到最终的textCollection,就是数据填完的textCollection-.-,这样填一个数据点show也会出textCollection。。-。-再帮帮忙,改下下吧^^
yixianggao 2007-08-13
  • 打赏
  • 举报
回复
To bcc1o()

JS 好好玩滴,哈
bcc1o 2007-08-13
  • 打赏
  • 举报
回复
JS好复杂
gzdiablo 2007-08-13
  • 打赏
  • 举报
回复
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);
yixianggao 2007-08-13
  • 打赏
  • 举报
回复
怎么用个input框把textCollection给显示出来?

L@_@K

<!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>
<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>
a2bb 2007-08-13
  • 打赏
  • 举报
回复
对了。。还要问下。。怎么用个input框把textCollection给显示出来
yixianggao 2007-08-13
  • 打赏
  • 举报
回复
lz 别客气,赶紧 sf 吧,哈
a2bb 2007-08-13
  • 打赏
  • 举报
回复
^^就是楼上这样,3Q啦。。过下来发分^^感谢各位。。。
yixianggao 2007-08-13
  • 打赏
  • 举报
回复
修正一下,L@_@K

<!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>
<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>
gzdiablo 2007-08-13
  • 打赏
  • 举报
回复
[form] 是 表单元素<form name="xxx">........</form>的name
如果不知道可以用 document代替
gzdiablo 2007-08-13
  • 打赏
  • 举报
回复
如果是按name排列..

var inputCollection = new Array();
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就是集合
yixianggao 2007-08-13
  • 打赏
  • 举报
回复
Quickly L@_@K

<!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>
<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>
mingxuan3000 2007-08-13
  • 打赏
  • 举报
回复
var a=document.getElementsByTagName("input")
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)
}
}
}
gzdiablo 2007-08-13
  • 打赏
  • 举报
回复
你指的顺序是 按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);
}

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧