The usage of radio buttons in JavaScript is similar to that of check boxes. The difference lies in the application in HTML. A checkbox is a switch. If a checkbox is selected, you can click it again to deselect it. But if a radio button is selected, you can only deselect it by selecting another radio button. Example:
In this example, if you want to deselect a radio button, you must click on another radio button. Look at the following program again:
When the first radio button box is selected, the function offButton() is called . The function is as follows:
function offButton()
{
var the_box = window.document.form_1.radio_1;
if (the_box.checked == true)
{
window.document.form_1.radio_2.checked = false;
This example is very similar to the previous checkbox example. The main difference is this line:
window.document.form_1.radio_2.checked = false;
This line Directive instructs JavaScript to close another button when this button is clicked. Since the function of another button is very similar to this one:
function onButton()
{var the_box = window.document.form_1.radio_2;
if (the_box.checked == true)
{
window.document.form_1.radio_1.checked = false; }
Menu is the most peculiar form option we have learned. There are two basic formats: following menus and list menus. The following is an example:
< ;option>spider
;
>
Note that the name of this menu is pulldown_1, but each option has no name. So it's a bit difficult to call each of the options.
Fortunately, arrays can help us call the options. If you want to change the second option in the following menu, you can do this:
window.document.form_1.pulldown_1.options[1].text = 'new_text';
This is because the menu element has an options attribute, which is an array of all options in the menu. Click change the selectt and then use the drop-down menu to see if its selection has been changed. Now the 2nd option should be *thau*.
In addition to the option attribute, the menu also has an attribute called selectedIndex. After an option is selected, the selectedIndex attribute will become the array index number (serial number) of the selected option. Select an option in the second list menu and check the index number. Remember that the first option in the array has index 0.
The name of the form is form_1, and the name of the list menu is list_1. The selectedIndex attribute value is window.document.form_1.list_1.selectedIndex. You can also
set the selectedIndex as follows: window.document.form_1.list_1.selectedIndex = 1; and highlight the second option. Once you get the index number of the selected item, you can find its content:
var the_select = window.document.form_1.list_1;
var the_index = the_select. selectedIndex;
var the_selected = the_select.options[the_index].text;
The selectedIndex attribute is useful, but what happens if multiple options are selected at the same time?
The handler of the menu element is onChange(). When the menu changes, the processor is activated.
Try this example and read the comments below:
My favorite animal is...
Comment a relatively complex JavaScript program. First, let’s look at the form itself: