Javascript is a drop-down list of provincial/municipal connections.

Source: Internet
Author: User

Javascript is a drop-down list of provincial/municipal connections.
Zookeeper


Object. options. add (new Option (label, value) method to add an option object to the Set;
The object. options. remove (index) method removes the specified items in the options set;
Object. options (index) or options. item (index) can be used to obtain the specified items in the options set through the index;

The select tag also has an attribute selectedIndex, which may be used to obtain the selected option index: object. selectedIndex

// 1. clear the options set function optionsClear (object) {var length = object. options. length; for (var I = length-1; I> = 0; I --) {e. options. remove (I) ;}// 2. add a new optionfunction addOption (object) {object. add (new Option (label, value); // use the last item in the options set to obtain the focus object. selectedIndex = object. lentht-1;} // 3. delete the specified optionfunction removeOption (index) {if (index> = 0) {object. remove (index); // use the last item in the options set to obtain the focus object. selec TedIndex = object. lentht-1;} // 4. obtain the real value of the currently selected option valuefunction getCurrentOptionValue (index) {if (index> = 0) return object (index ). value;} // 5. obtain the display value of the currently selected option labelfunction getCurrentOptionLabel (index) {if (index> = 0) return object (index ). text;} // The following is the date linkage drop-down box: function YYYYMMDD () {// set the month set MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // clear all content in the year drop-down box, including spaces var yObj = document. form1.yearopti OnsClear (yObj); // assign the content yObj to the year drop-down box. options. add (new Option ("select", 0); for (var I = 1950; I <= new Date (). getFullYear (); I ++) // The document ended this year based on January 1, 1950. form1.year. options. add (new Option (I, I); // clear all content in the month drop-down box, mainly including spaces var mObj = document. form1.month; optionsClear (mObj); // the drop-down list of the month to be assigned. form1.month. options. add (new Option ("select", 0); for (var I = 1; I <13; I ++) document. form1.month. options. add (new Option (I, I) ); // Specify the initial value of the year document. form1.year. value = document. getElementsByName ("YYYY") [0]. value; // specifies the initial value of the month document. form1.month. value = document. getElementsByName ("MM") [0]. value; // assign a date to the var n = MonHead [document. form1.month. value-1]; if (document. form1.month. value = 2 & IsPinYear (YYYYvalue) n ++; writeDay (n); // the initial value of the date assigned to the document. form1.date. value = document. getElementsByName ("DD") [0]. value;} // Date changes when the year changes (mainly to determine the year of the week) fun Ction YYYYDD (str) {var MMvalue = document. form1.month. options [document. form1.month. selectedIndex]. value; if (MMvalue = "") {var e = document. form1.date; optionsClear (e); return;} var n = MonHead [MMvalue-1]; if (MMvalue = 2 & IsPinYear (str) n ++; writeDay (n)} // The date linkage function MMDD (str) {var YYYYvalue = document. form1.year. options [document. form1.year. selectedIndex]. value; if (YYYYvalue = "") {var e = Document. form1.date; optionsClear (e); return;} var n = MonHead [str-1]; if (str = 2 & IsPinYear (YYYYvalue) n ++; writeDay (n)} // function writeDay (n) {var e = document. form1.date; optionsClear (e); for (var I = 1; I <(n + 1); I ++) e. options. add (new Option (I, I);} // determines whether the function IsPinYear (year) {return (0 = year % 4 & (year0! = 0 | year @ 0 = 0);} // clear all content in the drop-down box function optionsClear (e) {var length = object. options. length; for (var I = length-1; I> = 0; I --) {e. options. remove (I );}}
More benefits
// 1 check whether any selected if (objSelect. selectedIndex>-1) {// indicates selected} else {// indicates not selected} // 2 Delete the selected item objSelect. options [objSelect. selectedIndex] = null; // 3 add item objSelect. options [objSelect. length] = new Option ("hello", "hello"); // 4 modify the selected item objSelect. options [objSelect. selectedIndex] = new Option ("hello", "hello"); // 5 get the selected text objSelect. options [objSelect. selectedIndex]. text; // 6 get the value of the selected item objSelect. options [objSelect. selectedIndex]. value;


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.