JS Code is often used in Web programming and has powerful functions. The following is the common JS Code in my work, which is summarized as follows:
1. Trigger the event staff to submit the form
Example 1:
<Script language = "JavaScript">
Function tjsub (){
If (confirm ('Are you sure you want to submit this form? '))
Return document. form1.action = "test. php? Action = set ";
Frm1.submit ();
Else return false;
}
</SCRIPT>
<Form name = "form1" method = "Post">
<Input type = "text" name = "text1" value = "test text 1">
<Input type = "text" name = "text2" value = "test text 2" onclick = "tjsub ()>
<Input type = "Submit" name = "submit1" value = "Submit" ">
</Form>
2. onchange and onclick event triggering
Onchange is mostly used in trigger events in the drop-down list, with the focus on "change", that is, trigger events. Such as page Jump. Onclick emphasizes "click", that is, click triggers events, which is widely used.
It is easy to use and will not be used as an example.
3. onblur triggers an action when the focus leaves the text control. It is often used together with automatic focus acquisition.
4. automatically obtain the focus
Example 4:
<Body bgcolor = "# ffffff" onLoad = "document. form1.text2. Focus ()">
<Form name = "form1" method = "Post">
<Input type = "text" name = "text1" value = "test text 1">
<Input type = "text" name = "text2" value = "test text 2" onclick = "tjsub ()>
<Input type = "Submit" name = "submit1" value = "Submit" ">
</Form>
</Body>
5. Press enter to move the focus (onkeydown = "If (event. keycode = 13) event. keycode = 9 ")
Example 5:
<Form name = "form1" method = "Post">
<Input type = "text" name = "text1" value = "test text 1" onkeydown = "If (event. keycode = 13) event. keycode = 9">
<Input type = "text" name = "text2" value = "test text 2" onkeydown = "If (event. keycode = 13) event. keycode = 9">
<Input type = "Submit" name = "submit1" value = "Submit" ">
</Form>
6. Lock the text box (readonly)
Example 6:
<Form name = "form1" method = "Post">
<Input type = "text" name = "text1" value = "test text 1" readonly>
<Input type = "text" name = "text2" value = "test text 2" readonly>
<Input type = "Submit" name = "submit1" value = "Submit" ">
</Form>
7. lowercase letters are automatically converted to uppercase letters in the text box.
Example 7:
<Form name = "form1" method = "Post">
<Input type = "text" name = "text1" value = "sample" style = "font-variant: Small-Caps">
<Input type = "text" name = "text2" style = "font-variant: Small-Caps">
<Input type = "Submit" name = "submit1" value = "Submit" ">
</Form>
8. After the script locates the focus in a text box, how can I position the cursor in the text box before the string expires?
Example 8:
<SCRIPT>
Function setcaretatend (field ){
If (field. createTextRange ){
VaR r = field. createTextRange ();
R. movestart ('character ', field. value. Length );
R. Collapse ();
R. Select ();
}
}
</SCRIPT>
<Form name = "AForm">
<Input type = "text" name = "username" value = "" onfocus = "setcaretatend (this)">
<Input type = "text" name = "userage" value = "" onfocus = "setcaretatend (this)">
</Form>
For the time being ........