Iterate through <select> options

前端 未结 8 1284
不思量自难忘°
不思量自难忘° 2020-11-28 03:00

I have a

提交评论

  • 2020-11-28 03:16

    Another variation on the already proposed answers without jQuery.

    Object.values(document.getElementById('mySelect').options).forEach(option => alert(option))
    
    0 讨论(0)
  • 2020-11-28 03:17
    $("#selectId > option").each(function() {
        alert(this.text + ' ' + this.value);
    });
    
    • http://api.jquery.com/each/
    • http://jsfiddle.net/Rx3AP/
    0 讨论(0)
  • 2020-11-28 03:23

    This worked for me

    $(function() {
        $("#select option").each(function(i){
            alert($(this).text() + " : " + $(this).val());
        });
    });
    
    0 讨论(0)
  • 2020-11-28 03:28

    can also Use parameterized each with index and the element.

    $('#selectIntegrationConf').find('option').each(function(index,element){
     console.log(index);
     console.log(element.value);
     console.log(element.text);
     });
    

    // this will also work

    $('#selectIntegrationConf option').each(function(index,element){
     console.log(index);
     console.log(element.value);
     console.log(element.text);
     });
    
    0 讨论(0)
  • 2020-11-28 03:29

    And the requisite, non-jquery way, for followers, since google seems to send everyone here:

      var select = document.getElementById("select_id");
      for (var i = 0; i < select.length; i++){
        var option = select.options[i];
        // now have option.text, option.value
      }
    
    0 讨论(0)
  • 提交回复
    热议问题