Assume that the textbox1 character is copied to textbox2 in real time. The Code is as follows:
<Script language = "JavaScript">
Function copystr ()
{
Document. getelementbyid ("textbox2"). value = Document. getelementbyid ("textbox1"). value;
}
</SCRIPT>
<Form name = "form1" method = "Post" Action = "default. aspx" id = "form1">
<Input name = "textbox1" type = "text" id = "textbox1" onkeyup = "copystr ()"/> <br/>
<Input name = "textbox2" type = "text" id = "textbox2"/>
</Form>
The above code can work well. However, I think the document on the equal sign side 2. getelementbyid ("... ") the code is too long. You want to assign them to two variables before using them. Another benefit is that when document. getelementbyid ("...") is used in multiple statements, the amount of code saved is considerable. In addition, if document. getelementbyid ("...") is to be modified, only one statement on the front can be modified. Then rewrite it:
VaR obox1 = Document. getelementbyid ("textbox1 ");
VaR obox2 = Document. getelementbyid ("textbox2 ");
Obox2.value = obox1.value;
However, it is strange that the program does not achieve the expected results after execution.
Originally, obox2 itself is an object reference type. To assign a value to its attributes, you must initialize it before using it. Use the following code:
VaR obox1 = Document. getelementbyid ("textbox1 ");
VaR obox2 = new object (); // declare object variables
Obox2 = Document. getelementbyid ("textbox2 ");
Obox2.value = obox1.value;
-- Computer Learning Network (http://www.why100000.com)
2007-4-8