Operate the Select Control in JavaScript (ADD, modify, delete, select, clear, and determine existence)

Source: Internet
Author: User

1. Determine whether the select option contains an item with value = "paravalue"
2. add an item to the select option
3. delete an item from the select option
4. Delete the selected items in the SELECT statement.
5. Modify the text of value = "paravalue" in the select option to "paratext"
6. Set the first item of text = "paratext" in select to be selected.
7. Set the item of value = "paravalue" in select to be selected.
8. Obtain the value of the selected item in the SELECT statement.
9. Get the text of the selected item of the SELECT statement.
10. Obtain the index of the selected item of the SELECT statement.
11. Clear select items

 

JS Code
  1. JS Code
  2. // 1. Determine whether the select option contains an item with value = "paravalue"
  3. FunctionJsselectisexititem (objselect, objitemvalue ){
  4. VaRIsexit =False;
  5. For(VaRI = 0; I <objselect. Options. length; I ++ ){
  6. If(Objselect. Options [I]. value = objitemvalue ){
  7. Isexit =True;
  8. Break;
  9. }
  10. }
  11. ReturnIsexit;
  12. }
  13. // 2. add an item to the select option
  14. FunctionJsadditemtoselect (objselect, objitemtext, objitemvalue ){
  15. // Determine whether the object exists
  16. If(Jsselectisexititem (objselect, objitemvalue )){
  17. Alert ("The value of this item already exists");
  18. }Else{
  19. VaRVaritem =NewOption (objitemtext, objitemvalue );
  20. Objselect. Options. Add (varitem );
  21. Alert ("Successfully added");
  22. }
  23. }
  24. // 3. delete an item from the select option
  25. FunctionJsremoveitemfromselect (objselect, objitemvalue ){
  26. // Determine whether the object exists
  27. If(Jsselectisexititem (objselect, objitemvalue )){
  28. For(VaRI = 0; I <objselect. Options. length; I ++ ){
  29. If(Objselect. Options [I]. value = objitemvalue ){
  30. Objselect. Options. Remove (I );
  31. Break;
  32. }
  33. }
  34. Alert ("Deleted");
  35. }Else{
  36. Alert ("This select item does not exist");
  37. }
  38. }
  39. // 4. Delete the selected items in the SELECT statement.
  40. FunctionJsremoveselecteditemfromselect (objselect ){
  41. VaRLength = objselect. Options. Length-1;
  42. For(VaRI = length; I> = 0; I --){
  43. If(Objselect [I]. Selected =True){
  44. Objselect. Options [I] =Null;
  45. }
  46. }
  47. }
  48. // 5. Modify the text of value = "paravalue" in the select option to "paratext"
  49. FunctionJsupdateitemtoselect (objselect, objitemtext, objitemvalue ){
  50. // Determine whether the object exists
  51. If(Jsselectisexititem (objselect, objitemvalue )){
  52. For(VaRI = 0; I <objselect. Options. length; I ++ ){
  53. If(Objselect. Options [I]. value = objitemvalue ){
  54. Objselect. Options [I]. Text = objitemtext;
  55. Break;
  56. }
  57. }
  58. Alert ("Modified successfully");
  59. }Else{
  60. Alert ("This select item does not exist");
  61. }
  62. }
  63. // 6. Set the first item of text = "paratext" in select to be selected
  64. FunctionJsselectitembyvalue (objselect, objitemtext ){
  65. // Determine whether the object exists
  66. VaRIsexit =False;
  67. For(VaRI = 0; I <objselect. Options. length; I ++ ){
  68. If(Objselect. Options [I]. Text = objitemtext ){
  69. Objselect. Options [I]. Selected =True;
  70. Isexit =True;
  71. Break;
  72. }
  73. }
  74. // Show the result
  75. If(Isexit ){
  76. Alert ("Selected");
  77. }Else{
  78. Alert ("This select item does not exist");
  79. }
  80. }
  81. // 7. Set the item of value = "paravalue" in select to be selected
  82. Document. All. objselect. value = objitemvalue;
  83. // 8. Obtain the value of the selected item of the SELECT statement.
  84. VaRCurrselectvalue = Document. All. objselect. value;
  85. // 9. Obtain the text of the selected item of the SELECT statement.
  86. VaRCurrselecttext = Document. All. objselect. Options
  87. [Document. All. objselect. selectedindex]. text;
  88. // 10. Obtain the index of the selected item of the SELECT statement.
  89. VaRCurrselectindex = Document. All. objselect. selectedindex;
  90. // 11. Clear select items
  91. Document. All. objselect. Options. Length = 0;
JS Code // 1. determine whether the select option contains the item Function 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 item Function 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. add (varitem); alert ("successfully added") ;}// 3. delete an item Function 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. delete the function jsremoveselecteditemfromselect (objselect) {var length = objselect. options. length-1; for (VAR I = length; I> = 0; I --) {If (objselect [I]. selected = true) {objselect. options [I] = NULL ;}}// 5. 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 option does not exist") ;}}// 6. 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") ;}// 7. set item of value = "paravalue" in select to selected document. all. objselect. value = objitemvalue; // 8. obtain the value var currselectvalue = Document of the selected item in the SELECT statement. all. objselect. value; // 9. the text var currselecttext = document. all. objselect. options [document. all. objselect. selectedindex]. text; // 10. the index var currselectindex = document. all. objselect. selectedindex; // 11. clear the select item Document. all. objselect. options. length = 0;

Related Article

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.