This example describes the way JS uses the DOM to set radio buttons, check boxes, and Drop-down menus. Share to everyone for your reference. The implementation methods are as follows:
1. Set radio button
The radio button in the form is <input type= "Radio"/> It is a set of objects for the user to select, but only one at a time. Each has a checked attribute, and when one selection is ture, the other changes to false.
Put an example first:
Copy Code code as follows:
<script type= "Text/javascript" >
function Getchoice () {
var oform = document.forms["UForm1"];
var achoices = Oform.camera;
for (i = 0; i < achoices.length i++)//traverse the entire single option table
if (achoices[i].checked)//If the selected item is found, exit
Break
Alert ("Camera brand is:" + achoices[i].value);
}
function Setchoice (inum) {
var oform = document.forms["UForm1"];
Oform.camera[inum].checked = true;
}
</script>
<form method= "POST" name= "UForm1" action= "addinfo.aspx" >
Camera Brand:
<p>
<input type= "Radio" name= "Camera" id= "canon" value= "Canon" >
<label for= "Canon" >Canon</label>
</p>
<p>
<input type= "Radio" name= "Camera" id= "Nikon" value= "Nikon" >
<label for= "Nikon" >Nikon</label>
</p>
<p>
<input type= "Radio" name= "Camera" id= "Sony" Value= "Sony" Checked>
<label for= "Sony" >Sony</label>
</p>
<p>
<input type= "Radio" name= "Camera" id= "Olympus" Value= "Olympus" >
<label for= "Olympus" >Olympus</label>
</p>
<p>
<input type= "Radio" name= "Camera" id= "Samsung" value= "Samsung" >
<label for= "Samsung" >Samsung</label>
</p>
<p>
<input type= "Radio" name= "Camera" id= "Pentax" value= "Pentax" >
<label for= "Pentax" >Pentax</label>
</p>
<p>
<input type= "Radio" name= "Camera" id= "others" value= "other" >
<label for= "Others" >others</label>
</p>
<p>
<input type= "Submit" Name= "Btnsubmit" id= "btnsubmit" value= "Submit" class= "BTN" >
</p>
<p>
<input type= "button" value= "detects the selected object" onclick= "Getchoice ();" >
<input type= "button" value= "set to Canon" onclick= "Setchoice (0);" >
</p>
</form>
The radio button in the form is <input type= "Radio"/> It is a set of objects for the user to select, but only one at a time. Each has a checked attribute, and when one selection is ture, the other changes to false.
As you can see from the code above, IDs and name are different, and their name is the same in a set of radio buttons, and only one is selected. The ID is binding <label> or other selective action.
Where code: The code that checks for the selected object is (when the chcked value of an item is ture, the traversal ends)
Copy Code code as follows:
var oform = document.forms["UForm1"];
var achoices = Oform.camera;
for (i = 0; i < achoices.length i++)//traverse the entire single option table
if (achoices[i].checked)//If the selected item is found, exit
Break
Alert ("Camera brand is:" + achoices[i].value);
2. Set multiple selection boxes
Unlike radio buttons, the checkbox <input type= "checkbox"/> can be selected at the same time with multiple options for processing, the check box before each message in the mailbox is typically used
Copy Code code as follows:
<script type= "Text/javascript" >
function checkbox () {
var str = document.getelementsbyname ("hobby");
var objarray = str.length;
var chestr = "";
for (j = 0; J < Objarray; J + +) {
if (str[j].checked = = True) {
Chestr + + Str[j].value + ",";
}
}
if (Chestr = = "") {
Alert ("Please first choose a hobby ~! ");
} else {
Alert ("Your first choice is:" + chestr);
}
}
function Changeboxes (action) {
var oform = document.forms["MyForm1"];
var ocheckbox = Oform.hobby;
for (var i = 0; i < ocheckbox.length i++)//traverse each option
if (Action < 0)//anti-election
ocheckbox[i].checked =!ocheckbox[i].checked;
Else//action 1 is all selected, 0 is all not selected
ocheckbox[i].checked = action;
}
</script>
<form method= "POST" name= "MyForm1" action= "addinfo.aspx" >
Something you like to do:
<p>
<input type= "checkbox" name= "hobby" id= "Ball" value= "ball" >
<label for= "Ball" > Play </label>
</p>
<p>
<input type= "checkbox" name= "Hobby" id= "TV" value= "TV" >
<label for= "TV" > Watch TV </label>
</p>
<p>
<input type= "checkbox" name= "hobby" id= "NET" value= "NET" >
<label for= "NET" > Internet access </label>
</p>
<p>
<input type= "checkbox" name= "Hobby" id= "book" value= "book" >
<label for= "book" > Reading </label>
</p>
<p>
<input type= "checkbox" name= "hobby" id= "trips" value= "Trip" >
<label for= "Trip" > Tourism </label>
</p>
<p>
<input type= "checkbox" name= "Hobby" id= "Music" value= "Music" >
<label for= "Music" > Musical </label>
</p>
<p>
<input type= "checkbox" name= "Hobby" id= "others" value= "other" >
<label for= "Others" > Other </label>
</p>
<p>
<input type= "button" value= "All selected" onclick= "changeboxes (1);"/>
<input type= "button" value= "All do not choose" onclick= "changeboxes (0);"/>
<input type= "button" value= "onclick=" changeboxes ( -1); "/>
<input type= "button" value= "Submit" onclick= checkbox () "/>
</p>
</form>
The checkbox principle is determined by using the Boolean value of the checked attribute, which can be passed by 0 and 1.
3. Drop down Menu
Drop-down Menu <select> is a more commonly used form element. When its drop-down is a radio, and the radio button <input type= "Radio"/> function, the Drop-down menu is a check box when multiple= "multiple" for multiple selections, but the area is much smaller than the check box.
Common Properties for Drop-down menus:
| Property |
Description |
| Length |
Indicates options <option> numbers |
| Selected |
A Boolean value that indicates whether <option> is selected |
| SelectedIndex |
The serial number of the selected option, or 1 if no option is selected, and for the multiple-selection pull-down menu, returns the first selected
The ordinal number, counting starting from 0 |
| Text |
Text of the option |
| Value |
The value of the option |
| Type |
Type of Drop-down menu, Radio return select-one, multiple selection return select-multiple |
| Options |
Gets an array of options, such as: Oselectbox.options[2], which represents the Drop-down menu Oselectbox the third item |
①. Drop down menu to get a radio value
Copy Code code as follows:
<script language= "JavaScript" >
function Checksingle () {
var oform = document.forms["MyForm1"];
var oselectbox = oform.constellation;
var ichoice = Oselectbox.selectedindex; Get checked items
Alert ("You have selected" + Oselectbox.options[ichoice].text);
}
</script>
<form method= "POST" name= "MyForm1" >
<label for= "Constellation" > Constellation:</label>
<p>
<select id= "Constellation" name= "Constellation" >
<option value= "Aries" selected= "selected" > Aries </option>
<option value= "Taurus" > Taurus </option>
<option value= "Gemini" > Gemini </option>
<option value= "Cancer" > Cancer </option>
<option value= "Leo" > Lion </option>
<option value= "Virgo" > Virgin </option>
<option value= "Libra" > Libra </option>
<option value= "Scorpio" > Scorpio </option>
<option value= "Sagittarius" > Archer </option>
<option value= "Capricorn" > Capricorn </option>
<option value= "Aquarius" > Aquarius </option>
<option value= "Pisces" > Pisces </option>
</select>
</p>
<input type= "button" onclick= "Checksingle ()" value= "View Options"/>
</form>
②. Drop-down menu for multiple selections, take a value
Copy Code code as follows:
<script type= "Text/javascript" >
function Checkmultiple () {
var oform = document.forms["MyForm1"];
var oselectbox = oform.constellation;
var achoices = new Array ();
Traverse the entire Drop-down menu
for (var i = 0; i < oSelectBox.options.length; i++)
if (oselectbox.options[i].selected)//If selected
Achoices.push (Oselectbox.options[i].text); Push into the array
Alert ("You have selected:" + achoices.join ()); Output results
}
</script>
<form method= "POST" name= "MyForm1" >
<label for= "Constellation" > Constellation:</label>
<p>
<select id= "Constellation" name= "Constellation" multiple= "multiple" style= "height:180px"; >
<option value= "Aries" > Aries </option>
<option value= "Taurus" > Taurus </option>
<option value= "Gemini" > Gemini </option>
<option value= "Cancer" > Cancer </option>
<option value= "Leo" > Lion </option>
<option value= "Virgo" > Virgin </option>
<option value= "Libra" > Libra </option>
<option value= "Scorpio" > Scorpio </option>
<option value= "Sagittarius" > Archer </option>
<option value= "Capricorn" > Capricorn </option>
<option value= "Aquarius" > Aquarius </option>
<option value= "Pisces" > Pisces </option>
</select>
</p>
<input type= "button" onclick= "Checkmultiple ()" value= "View Options"/>
</form>
③. Universal values (lower Lathan and multiple selections)
Copy Code code as follows:
<script language= "JavaScript" >
function Getselectvalue (Box) {
var oform = document.forms["MyForm1"];
var oselectbox = Oform.elements[box]; Select the Drop-down menu according to the corresponding parameters
if (Oselectbox.type = = "Select-one") {//Judge is a radio or multiple selection
var ichoice = Oselectbox.selectedindex; Get checked items
Alert ("Radio, you have selected" + Oselectbox.options[ichoice].text);
} else {
var achoices = new Array ();
Traverse the entire Drop-down menu
for (var i = 0; i < oSelectBox.options.length; i++)
if (oselectbox.options[i].selected)//If selected
Achoices.push (Oselectbox.options[i].text); Push into the array
Alert ("Multiple selection, you have selected:" + achoices.join ()); Output results
}
}
</script>
<form method= "POST" name= "MyForm1" >
Constellation:
<p>
<select id= "Constellation1" name= "Constellation1" >
<option value= "Aries" selected= "selected" > Aries </option>
<option value= "Taurus" > Taurus </option>
<option value= "Gemini" > Gemini </option>
<option value= "Cancer" > Cancer </option>
<option value= "Leo" > Lion </option>
<option value= "Virgo" > Virgin </option>
<option value= "Libra" > Libra </option>
<option value= "Scorpio" > Scorpio </option>
<option value= "Sagittarius" > Archer </option>
<option value= "Capricorn" > Capricorn </option>
<option value= "Aquarius" > Aquarius </option>
<option value= "Pisces" > Pisces </option>
</select>
<input type= "button" onclick= "Getselectvalue (' Constellation1 ')" value= "View Options"/>
</p>
<p>
<select id= "Constellation2" name= "Constellation2" multiple= "multiple" style= "height:120px"; >
<option value= "Aries" > Aries </option>
<option value= "Taurus" > Taurus </option>
<option value= "Gemini" > Gemini </option>
<option value= "Cancer" > Cancer </option>
<option value= "Leo" > Lion </option>
<option value= "Virgo" > Virgin </option>
<option value= "Libra" > Libra </option>
<option value= "Scorpio" > Scorpio </option>
<option value= "Sagittarius" > Archer </option>
<option value= "Capricorn" > Capricorn </option>
<option value= "Aquarius" > Aquarius </option>
<option value= "Pisces" > Pisces </option>
</select>
<input type= "button" onclick= "Getselectvalue (' Constellation2 ')" value= "View Options"/>
</p>
</form>
I hope this article will help you with your JavaScript programming.