JavaScript Tutorial/Form/CheckBox
Содержание
- 1 A Checkbox and an onclick Event Handler
- 2 A Checkbox, an onclick Event Handler and show/hide form controls
- 3 Checkbox
- 4 Checkbox.blur()
- 5 Checkbox.checked
- 6 Checkbox.click()
- 7 Checkbox.defaultChecked
- 8 Checkbox.focus()
- 9 Checkbox.form
- 10 Checkbox.handleEvent()
- 11 Checkbox.name
- 12 Checkbox.onBlur
- 13 Checkbox.onClick
- 14 Checkbox.onFocus
- 15 Checkbox.type
- 16 Checkbox.value
- 17 Check if a CheckBox checked
- 18 Use checkbox to control the window style
A Checkbox and an onclick Event Handler
<source lang="javascript">
<html> <head> <title>Checkbox Event Handler</title> <style type="text/css">
- myGroup {visibility:hidden}
</style> <script type="text/javascript"> function toggle(chkbox, group) {
var visSetting = (chkbox.checked) ? "visible" : "hidden"; document.getElementById(group).style.visibility = visSetting;
} function swap(radBtn, group) {
var modemsVisSetting = (group == "modems") ? ((radBtn.checked) ? "" : "none") : "none"; document.getElementById("modems").style.display = modemsVisSetting;
} </script> </head> <body> <form> <input type="checkbox" name="monitor" onclick="toggle(this, "myGroup")" />Monitor
<input type="radio" />15"
</form> </body> </html></source>
A Checkbox, an onclick Event Handler and show/hide form controls
<source lang="javascript">
<html> <head> <title>Checkbox Event Handler</title> <style type="text/css">
- myGroup {visibility:hidden}
</style> <script type="text/javascript"> function toggle(chkbox, group) {
var visSetting = (chkbox.checked) ? "visible" : "hidden"; document.getElementById(group).style.visibility = visSetting;
} function swap(radBtn, group) {
var modemsVisSetting = (group == "modems") ? ((radBtn.checked) ? "" : "none") : "none"; document.getElementById("modems").style.display = modemsVisSetting;
} </script> </head> <body> <form> <input type="checkbox" name="monitor" onclick="toggle(this, "myGroup")" />Monitor
<input type="radio" />15"
</form> </body> </html></source>
Checkbox
The Checkbox object represents a graphical check box.
Check boxes are created as part of a form by using the <input> tag with the TYPE attribute set to checkbox in an HTML document.
Once created, check boxes can be accessed in JavaScript as an element of a form using dot notation.
Check boxes can also be grouped together under the same name and accessed as an array by using brackets.
Arguments, Properties, Methods, and Event Handlers Associated with the Checkbox Object are listed in the following table.
checked A Boolean value that determines if the check box is checked. defaultChecked A Boolean value that holds the initial state of the check box. This value is set with the CHECKED attribute. form Returns the Form object of the check box. name The string specified in the NAME attribute of the HTML <input> tag. type The string specified in the TYPE attribute of the HTML <input> tag. This string is always checkbox for the Checkbox object. value The value returned when the form is submitted. blur() Removes focus from the check box. click() Calls the check box"s onClick event handler. focus() Applies focus to this check box. handleEvent() Passes an event to the appropriate event handler associated with the check box. onBlur The handler invoked when focus is removed from the check box. onClick The handler invoked when the check box is selected. onFocus The handler invoked when focus is applied to the check box.
<source lang="javascript">
<html>
<script language="JavaScript"> </script> <form name="orderForm"> Lettuce <input type="checkbox" value="lettuce" name="lettuceCB"><input type="checkbox" value="tomatoe"name="tomatoeCB">
Cheese <input type="checkbox" value="cheese" name="cheeseCB">
Tomatoe
Step 2: <input type="button" value="Submit Order" name="orderButton" onClick="submitOrder()"> </form> </html></source>
Checkbox.blur()
Syntax
<source lang="javascript">
document.form.checkbox.blur()</source>
Checkbox.checked
Syntax
<source lang="javascript">
document.form.checkbox.checked</source>
Checkbox.click()
Syntax
<source lang="javascript">
document.form.checkbox.click()</source>
Checkbox.defaultChecked
Syntax
<source lang="javascript">
document.form.checkbox.defaultChecked</source>
Checkbox.focus()
Syntax
<source lang="javascript">
document.form.checkbox.focus()</source>
Checkbox.form
Syntax
<source lang="javascript">
document.form.checkbox.form</source>
Checkbox.handleEvent()
Syntax
<source lang="javascript">
document.form.checkbox.handleEvent(event)</source>
The handleEvent() method provides a way to invoke a check box"s event handler, even though the event never happened.
Argument Associated with the handleEvent() Method
event An Event object to be handled
Checkbox.name
Syntax
<source lang="javascript">
document.form.checkbox.name</source>
Checkbox.onBlur
Syntax
<source lang="javascript">
onBlur="command"</source>
Checkbox.onClick
Syntax
<source lang="javascript">
onClick="command"</source>
Checkbox.onFocus
Syntax
<source lang="javascript">
onFocus="command"</source>
Checkbox.type
Syntax
<source lang="javascript">
document.form.checkbox.type</source>
Checkbox.value
Syntax
<source lang="javascript">
document.form.checkbox.value</source>
Check if a CheckBox checked
<source lang="javascript">
<html> <head> <title>Online Survey</title> <script type="text/javascript" language="javascript">
</script> </head> <body> <form action="http://www.wbex.ru" method="POST" name="MyForm">
Online Survey |
||
---|---|---|
Your Name: | <input type="text" name="YourName"/> | |
Your Gender: |
<input type="radio" name="Gender" value="Male"/>Male |
|
Which of our consultancy services are you interested in? |
<input type="checkbox" name="XML"/> XML |
|
Which free gift would you prefer for filling out this survey? |
<select name="FreeGift"> <option value="Choice1">Choice 1</option> <option value="Choice2">Choice 2</option> <option value="Choice3">Choice 3</option> </select> | |
Enter your comments in the text box |
<textarea name="Comments" rows="5" cols="50"></textarea> | |
<input type="submit" value="Send Form" onclick="CheckCheckboxes()"/> |
</form> </body> </html></source>
Use checkbox to control the window style
<source lang="javascript">
<html> <head> <title>Open Window</title> <script language="JavaScript" type =" text/javascript">
</script> </head> <body> <form name="form1">
<input type="checkbox" name="directories" value="0">Directories Option</p>
<input type="checkbox" name="location" value="0">Location Option
<input type="checkbox" name="menubar" value="0">Menu Bar
<input type="checkbox" name="resized" value="0">Allow Window to be Resized
<input type="checkbox" name="scrollbars" value="0">Scrollbars
<input type="checkbox" name="status" value="0">Status Bar
<input type="checkbox" name="toolbar" value="0">Toolbar
<input type="button" value="Create Window" name="CreateWin" onClick="ChangeLink(this.form)">
</form> </body> </html></source>