Home > Web Front-end > JS Tutorial > body text

JavaScript Beginner Tutorial (Lesson 5 Continued)_Basic Knowledge

PHP中文网
Release: 2016-05-16 19:15:22
Original
1147 people have browsed it

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:


Light off
Light on
 

  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:

The strange thing is that this menu has a name, but the options in it do not have names. For example, in HTML, the first menu is as follows:


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.

check the index.

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:


selectedIndex].text);">
                                             " option>
                                  ;/select>

🎜>                                                                             ;greyhound

                                                                                                                                                                            The handler for the following menus calls the function swapOptions(). This function has been defined in the header

, and its parameter is - the selected animal type.

The first few arrays I defined in the header:

var dogs = new Array("poodle","puli","greyhound");

var fish = new Array("trout", "mackerel", "bass");


var birds = new Array("robin", "hummingbird", "crow");

Pay attention to these arrays The naming is consistent with the naming in the drop-down menu. Soon you'll understand why. Now let’s look at the function that is called when the drop-down menu is changed:

function swapOptions(the_array_name)

{

var numbers_select = window.document.the_form.the_examples;

var the_array = eval(the_array_name);

setOptionText(window.document.the_form.the_examples, the_array);

}

The definition of this function includes one parameter: the_array_name. If you open the drop-down menu and select "Fish", the_array_name is equivalent to the string "Fish".


The first line in the function body includes a variable to reference the second form element: the list menu.

Line 2 introduces a new concept: eval(). eval() is rather strange, we will explain it in a later course. The result of these commands on line 2 is that the variable the_array will be equal to one of the arrays defined previously. If the_array_name is "Fish", the_array is equivalent to the Fish array. If you want to understand how this is done, learn eval.

Line 3 defines another function setOptionText(). setOptionText() is used to assign the_array to the list menu. The following is the function content:

function setOptionText(the_select, the_array)

{

for (loop=0; loop < the_select.options.length; loop )

{

the_select.options[loop].text = the_array[loop];

}

}

This function takes two parameters: a reference to the menu element and an array. Line 1 sets up a for loop to loop through all menu elements. Note that the options attribute of the menu element is an array consisting of all the options of the menu. Because it is an array, you can find the number of elements in the array using the length property. During the first loop, the loop variable value is 0. The main value of the loop is:

the_select.options[0].text = the_array[0];

If you select "Fish" in the drop-down menu, then the_array[0] is "trout", so this line of command changes the first option in the list menu to "trout". The next time it loops, the loop is equal to 1, and the second option in the list menu is "mackerel" .

If you understand this example, you have a deeper understanding of JavaScript.

This is the end of the basic tutorial, and advanced tutorials will be released later, so stay tuned.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!