Javascript options attribute set operation code

Source: Internet
Author: User

CopyCode The Code is as follows: <form name = "testform">
<Select name = "testselect">
<Option value = "first"> first option </option>
<Option value = "second"> second option </option>
<Option value = "third"> third option </option>
<Option> your browser can't handle this script </option>
</SELECT>
</Form>

Use the following code to access the options in the drop-down list:Copy codeThe Code is as follows: // get the option object
Document. Forms ['testform']. testselect. Options [I]

If you want to delete optionCopy codeThe Code is as follows: Document. Forms ['testform']. testselect. Options [I] = NULL;

If this option object is marked as null, this option is completely deleted from the list.
Note: This operation affects the number of options. Assume that you have deleted option [1] from the instance above, and the original option [2] element ('third option ') it will become the option [1] element (the option element goes top of the Order ).
Create a new option as follows:Copy codeThe Code is as follows: Document. Forms ['testform']. testselect. Options [I] = New Option ('new text', 'new value ');

On the page, you can see that the text and value displayed by option are the value attribute of this option.
When a form is submitted, the value is passed to the web server.
If you want to clear all options in the select box, as follows:Copy codeThe Code is as follows: Document. Forms ['testform']. testselect. Option. Length = 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.