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

求教,谁帮我看看这个代码,我JS没有基础,不知道对不对啊
<SCRIPT   language= "JavaScript ">
                                    var   cab=document.form1.cab.value;
                                    document.write( " <td   width=63   align=center> <font   size=2> <input   type=text   name=cabid   size=6   readonly   value= "+cab+ "> </font> </td> ");
                                    </SCRIPT>

这样子写,可不可以把form1里面的cab的value传给cabid?
我试了下,不可以,没有任何显示,如果要实现这个功能,我该怎么改代码呢?

------解决方案--------------------
可以,不过要注意js放置的位置

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<html>
<head>
<title> New Document </title>
<meta name= "Generator " content= "EditPlus ">
<meta name= "Author " content= " ">
<meta name= "Keywords " content= " ">
<meta name= "Description " content= " ">
</head>

<body>
<form method=post action= " " id= "form1 ">
<input type= "text " name= "cab " value= "abcd ">
<input type= "button " onclick= "alert(document.body.innerHTML); ">
</form>
</body>
</html>
<SCRIPT language= "JavaScript ">
var cab=document.forms( "form1 ").cab.value;
document.write( " <td width=63 align=center> <font size=2> <input type=text name=cabid size=6 readonly value= "+cab+ "> </font> </td> ");
</SCRIPT>


------解决方案--------------------
<SCRIPT language= "JavaScript ">
var cab=document.form1.cab.value;
document.write( " <td width=63 align=center> <font size=2> <input type=text name=cabid size=6 readonly value= "+cab+ "> </font> </td> ");
</SCRIPT>
这段代码放在cab这个对象之后就可以。因为html文件是从上到下进行编译执行的。