Home Web Front-end JS Tutorial JavaScript Beginner Tutorial (Lesson 5 Continued)_Basic Knowledge

JavaScript Beginner Tutorial (Lesson 5 Continued)_Basic Knowledge

May 16, 2016 pm 07:15 PM

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.



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

See all articles