为什么这段脚本失效?
下述代码中 "document.formfoo.txtHardDiskSN.value = hex(f.serialnumber)
"没有成功的执行,该文本框没有成功显示硬盘序列号,但 "document.write(hex(f.serialnumber)) "却能够成功打印这个硬盘序列号。何解?
<html>
<head>
<script language= "vbscript ">
set fs = CreateObject( "scripting.filesystemobject ")
set f = fs.GetDrive( "c: ")
document.write(hex(f.serialnumber))
document.formfoo.txtHardDiskSN.value = hex(f.serialnumber)
</script>
</head>
<body>
<form name= "formfoo "> <input value= "aaa " name= "txtHardDiskSN " type= "text "> </input> </FORM> </BODY>
</html>
[解决办法]
因为你的脚本写在了控件之前,脚本执行的时候控件还没加载。你把脚本写到控件后就可以了:
<html>
<head>
</head>
<body>
<form name= "formfoo "> <input value= "aaa " name= "txtHardDiskSN " type= "text "> </input> </FORM> </BODY>
<script language= "vbscript ">
set fs = CreateObject( "scripting.filesystemobject ")
set f = fs.GetDrive( "c: ")
document.write(hex(f.serialnumber))
document.all.formfoo.txtHardDiskSN.value = hex(f.serialnumber)
</script>
</html>