Turn JavaScript Operations Select Control Daquan (add, modify, delete, select, empty, Judge existence, etc.)

Source: Internet
Author: User

Collection

1. Determine if the value= "Paravalue" item exists in the SELECT option
2. Add an item to the Select option
3. Remove an item from the Select option
4. Delete selected items in select
5, modify the Select option Value= "Paravalue" text is "Paratext"
6. Set the first item of text= "Paratext" in Select to be selected
7. Set the "Paravalue" item in Select to Value=
8. Gets the value of the current selected item of select
9. Gets the text of the current selected item of select
10. Gets the index of the current selected item of select
11. Clear the Select item

    1. JS Code
    2. 1. Determine if the value= "Paravalue" item exists in the SELECT option
    3. function Jsselectisexititem (Objselect, Objitemvalue) {
    4. var isexit = false;
    5. For (var i = 0; i < objSelect.options.length; i++) {
    6. if (Objselect.options[i].value = = Objitemvalue) {
    7. Isexit = true;
    8. Break ;
    9. }
    10. }
    11. return isexit;
    12. }
    13. 2. Add an item to the Select option
    14. function Jsadditemtoselect (Objselect, Objitemtext, Objitemvalue) {
    15. //Determine if there is
    16. if (Jsselectisexititem (Objselect, Objitemvalue)) {
    17. Alert ("The value of the item already exists");
    18. } Else {
    19. var varitem = New Option (Objitemtext, Objitemvalue);
    20. ObjSelect.options.add (Varitem);
    21. Alert ("join successfully");
    22. }
    23. }
    24. 3. Remove an item from the Select option
    25. function Jsremoveitemfromselect (Objselect, Objitemvalue) {
    26. //Determine if there is
    27. if (Jsselectisexititem (Objselect, Objitemvalue)) {
    28. For (var i = 0; i < objSelect.options.length; i++) {
    29. if (Objselect.options[i].value = = Objitemvalue) {
    30. ObjSelect.options.remove (i);
    31. Break ;
    32. }
    33. }
    34. Alert ("delete successfully");
    35. } Else {
    36. Alert ("The item does not exist in this select");
    37. }
    38. }
    39. 4. Delete the selected item in select
    40. function Jsremoveselecteditemfromselect (objselect) {
    41. var length = objselect.options.length-1;
    42. For (var i = 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. function Jsupdateitemtoselect (Objselect, Objitemtext, Objitemvalue) {
    50. //Determine if there is
    51. if (Jsselectisexititem (Objselect, Objitemvalue)) {
    52. For (var i = 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 ("successful modification");
    59. } Else {
    60. Alert ("The item does not exist in this select");
    61. }
    62. }
    63. 6. Set the first item of text= "Paratext" in Select to be selected
    64. function Jsselectitembyvalue (Objselect, Objitemtext) {
    65. //Determine if there is
    66. var isexit = false;
    67. For (var i = 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 results
    75. if (isexit) {
    76. Alert ("selected successfully");
    77. } Else {
    78. Alert ("The item does not exist in this select");
    79. }
    80. }
    81. 7. Set the item of value= "Paravalue" in Select to select
    82. Document.all.objSelect.value = Objitemvalue;
    83. 8. Gets the value of the current selected item of select
    84. var currselectvalue = Document.all.objSelect.value;
    85. 9. Gets the text of the current selected item of select
    86. var currselecttext = Document.all.objselect.options[document.all.objselect.selectedindex].text;
    87. 10. Get the index of the current selected item of select
    88. var currselectindex = Document.all.objSelect.selectedIndex;
    89. 11. Clear the Select item
    90. document.all.objSelect.options.length = 0;

Turn JavaScript Operations Select Control Daquan (add, modify, delete, select, empty, Judge existence, etc.)

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.