Add, delete, and modify a SELECT statement in JS

Source: Internet
Author: User
1. check whether item2. of value = "paravalue" exists in the select option. Add item3. Delete item4. from the select option, modify the text of value = "paravalue" in the select option to "paravalue" paratext "5. set the first item of text = "paratext" in select to select 6. set item of value = "paravalue" in select to select 7. obtain the value8of the selected item in the SELECT statement. Obtain the text9of the selected item in the SELECT statement. Get the index10of the selected item in the SELECT statement. Clear the select item ----------------------------- // 1. determine whether itemfunction jsselectisexititem (objselect, objitemvalue) {var isexit = false; For (VAR I = 0; I <objselect. options. length; I ++) {If (objselect. options [I]. value = objitemvalue) {isexit = true; break;} return isexit;} // 2. add an itemfunction jsadditemtoselect (objselect, objitemtext, objitemvalue) to the select option {// determine whether there is if (jsselectisexititem (objselect, objitemvalue )) {alert ("the value of this item already exists");} else {var varitem = New Option (objitemtext, objitemvalue); // objselect. options [objselect. options. length] = varitem; objselect. options. add (varitem); alert ("successfully added") ;}// 3. delete An itemfunction jsremoveitemfromselect (objselect, objitemvalue) from the select option {// determine whether there is if (jsselectisexititem (objselect, objitemvalue )){
For (VAR I = 0; I <objselect. options. length; I ++) {If (objselect. options [I]. value = objitemvalue) {objselect. options. remove (I); break ;}} alert ("deleted successfully") ;}else {alert ("this select does not exist") ;}} // 4. modify the text of value = "paravalue" in the select option to "paratext" function jsupdateitemtoselect (objselect, objitemtext, objitemvalue) {// determine if (jsselectisexititem (objselect, objitemvalue )) {for (VAR I = 0; I <objselect. options. length; I ++) {If (objselect. options [I]. value = objitemvalue) {objselect. options [I]. TEXT = objitemtext; break; }} alert ("modified successfully ");}
Else {alert ("this select does not exist") ;}// 5. set the first item of text = "paratext" in select to the selected function jsselectitembyvalue (objselect, objitemtext) {// determine whether var isexit = false; For (VAR I = 0; I <objselect. options. length; I ++) {If (objselect. options [I]. TEXT = objitemtext) {objselect. options [I]. selected = true; isexit = true; break;} // show the result if (isexit) {alert ("selected successfully ");} else {alert ("this select does not exist") ;}// 6. set the item of value = "paravalue" in select to select // document. all. objselect. value = objitemvalue; // 7. obtain the value/var currselectvalue = Document of the selected item in the SELECT statement. all. objselect. value; // 8. the text // var currselecttext = document. all. objselect. options [document. all. objselect. selectedindex]. text; // 9. the index // var currselectindex = document. all. objselect. selectedindex; // 10. clear select items // document. all. objselect. options. 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.