Home >Web Front-end >JS Tutorial >javascript options attribute collection operation code_form effects

javascript options attribute collection operation code_form effects

WBOY
WBOYOriginal
2016-05-16 18:38:041227browse
Copy code The code is as follows:


< select name="testselect">


< ;option value="third">third option




Use the following code to access the options in the drop-down box:
Copy code The code is as follows:

// Get option object
document.forms['testform'].testselect.options[i]

If you want to delete option
Copy code The code is as follows:

document.forms['testform'].testselect.options[i] = null;

Mark this option object as null, and this option will be completely removed from the list.
Note: This operation will affect the number of options. Assume that in the above example, you delete option[1], the original option[2] element ('Third option') will become the option[1] element (option elements are pushed up in order).
Create a new option as follows:
Copy the code The code is as follows:

document .forms['testform'].testselect.options[i] = new Option('new text','new value');

The user sees the text and value displayed by the option on the page The value is the VALUE attribute of this option.
When the form is submitted, the VALUE value is passed to the WEB server.
If you want to clear all options in the select box, as follows:
Copy the code The code is as follows:

document.forms['testform'].testselect.option.length = 0;
Statement:
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