How to set selectedIndex of select element using display text?

后端 未结 7 937
北荒
北荒 2020-12-04 19:11

How to set selectedIndex of select element using display text as reference?

Example:



                        
    
提交评论

  • 2020-12-04 19:52

    You can set the index by this code :

    sel.selectedIndex = 0;
    

    but remember a caution in this practice, You would not be able to call the server side onclick method if you select the previous value selected in the drop down..

    0 讨论(0)
  • 2020-12-04 19:53

    If you want this without loops or jquery you could use the following This is straight up JavaScript. This works for current web browsers. Given the age of the question I am not sure if this would have worked back in 2011. Please note that using css style selectors is extremely powerful and can help shorten a lot of code.

    // Please note that querySelectorAll will return a match for 
    // for the term...if there is more than one then you will 
    // have to loop through the returned object
    var selectAnimal = function() {
      var animals = document.getElementById('animal');
      if (animals) {
        var x = animals.querySelectorAll('option[value="frog"]');
        if (x.length === 1) {
          console.log(x[0].index);
          animals.selectedIndex = x[0].index;
        }
      }
    }
    <html>
    
    <head>
      <title>Test without loop or jquery</title>
    </head>
    
    <body>
      <label>Animal to select
      <select id='animal'>
        <option value='nothing'></option>
        <option value='dog'>dog</option>
        <option value='cat'>cat</option>
        <option value='mouse'>mouse</option>
        <option value='rat'>rat</option>
        <option value='frog'>frog</option>
        <option value='horse'>horse</option>
      </select>
      </label>
      <button onclick="selectAnimal()">Click to select animal</button>
    
    </body>
    
    </html>

    document.getElementById('Animal').querySelectorAll('option[value="searchterm"]'); in the index object you can now do the following: x[0].index

    0 讨论(0)
  • 2020-12-04 19:54

    You can use the HTMLOptionsCollection.namedItem() That means that you have to define your select options to have a name attribute and have the value of the displayed text. e.g California

    0 讨论(0)
  • 2020-12-04 19:55

    Try this:

    function SelectAnimal()
    {
        var animals = document.getElementById('Animals');
        var animalsToFind = document.getElementById('AnimalToFind');
        // get the options length
        var len = animals.options.length;
        for(i = 0; i < len; i++)
        {
          // check the current option's text if it's the same with the input box
          if (animals.options[i].innerHTML == animalsToFind.value)
          {
             animals.selectedIndex = i;
             break;
          }     
        }
    }
    
    0 讨论(0)
  • 2020-12-04 19:56
    <script type="text/javascript">
         function SelectAnimal(){
             //Set selected option of Animals based on AnimalToFind value...
             var animalTofind = document.getElementById('AnimalToFind');
             var selection = document.getElementById('Animals');
    
            // select element
            for(var i=0;i<selection.options.length;i++){
                if (selection.options[i].innerHTML == animalTofind.value) {
                    selection.selectedIndex = i;
                    break;
                }
            }
         }
    </script>
    

    setting the selectedIndex property of the select tag will choose the correct item. it is a good idea of instead of comparing the two values (options innerHTML && animal value) you can use the indexOf() method or regular expression to select the correct option despite casing or presense of spaces

    selection.options[i].innerHTML.indexOf(animalTofind.value) != -1;
    

    or using .match(/regular expression/)

    0 讨论(0)
  • 提交回复
    热议问题