为什么运行时总说document.formname.hiddenName.length为未定义的?
实际上 document.voucher.choice.value是存在的呀?取一个hidden表单元素的长度应该怎么写?
var SID="";
alert(document.voucher.choice.length);//说未定义
for(i=0;i<document.voucher.choice.length;i++)
{
SID=document.voucher.choice.value+","+SID;
}
SID=SID.substring(0,SID.length-1);
问题点数:50、回复次数:11Top
1 楼fokker(独孤龙)回复于 2002-07-31 12:09:26 得分 7
改为:
document.voucher.choice.value.lengthTop
2 楼thinkover(JavaCoffee)回复于 2002-07-31 12:09:35 得分 7
把你的这一句:
alert(document.voucher.choice.length);
改为:
alert(document.voucher.choice.value.length);Top
3 楼net_lover(【孟子E章】)回复于 2002-07-31 12:32:54 得分 5
<input type=button onclick="test()" value=test>
<script>
function test()
{
for(var i=0;i<document.getElementsByTagName("INPUT").length;i++)
{
if(document.getElementsByTagName("INPUT")[i].type=="hidden")
alert(document.getElementsByTagName("INPUT")[0].value)
}
}
</script>Top
4 楼net_lover(【孟子E章】)回复于 2002-07-31 12:39:41 得分 5
<input type=button onclick="test()" value=test>
<input type=hidden value=test1>
<input type=hidden value=test2>
<script>
function test()
{
mxh=true
for(var i=0;i<document.getElementsByTagName("INPUT").length;i++)
{
if(document.getElementsByTagName("INPUT")[i].type=="hidden" && mxh)
{
alert(document.getElementsByTagName("INPUT")[i].value)
mxh=false
}
}
}
</script>Top
5 楼yundanfengqing(云淡风轻)回复于 2002-07-31 12:46:52 得分 0
我意思是说hidden中存放的不是一个记录的字段值,而是一系列记录的字段值,那么得要用一个循环才能把所有的值取出来,但是值的个数我不知道,所以要用document.voucher.choice.length控制。上面贴的代码有误:
var SID="";
alert(document.voucher.choice.length);//说未定义
for(i=0;i<document.voucher.choice.length;i++)
{
SID=document.voucher.choice[i].value+","+SID;
}
SID=SID.substring(0,SID.length-1);
谢谢!请GO UP!
Top
6 楼yundanfengqing(云淡风轻)回复于 2002-07-31 12:49:30 得分 0
OH?!Thank you,MXH!Top
7 楼Andrawu(晓彬)回复于 2002-07-31 13:38:19 得分 10
<input type=hidden name=h value=aa>
<input type=hidden name=h value=bb>
<input type=hidden name=s value=abcdef>
<script>
alert(document.all.h.length) ;
alert(document.all.h[0].value) ;
alert(document.all.h[1].value) ;
alert(document.all.s.value) ;
alert(document.all.s.value.length) ;
alert(document.all.s.length) ;
</script>
对于多个相同的元素名称可以用document.all.h.length得到元素的个数。
只有一个元素,如果用了document.all.s.length将会出现无定义。Top
8 楼duunn(syx)回复于 2002-07-31 15:04:27 得分 8
<input >无 id 属性时也会报这个错
www.onlylines.com
Top
9 楼runmin()回复于 2002-07-31 15:31:07 得分 8
document.voucher.choice.length
这样是取得对象的长度,而对象并没有定义这个属性,所以会提示未定义
document.voucher.choice.value.length
(document.voucher.choice.value).length
这样实际上是取得一个字符串对象的长度,字符串对象是有length属性的,所以这样才是正确的.Top
10 楼yundanfengqing(云淡风轻)回复于 2002-07-31 16:01:59 得分 0
Andrawu的测试程序真好理解。谢谢各位的解答!!Top
11 楼yundanfengqing(云淡风轻)回复于 2002-07-31 20:11:07 得分 0
还是孟子E章的代码实用性高。谢谢大家的指导使我明白了这个问题。Top




