Get current value selected in dropdown using jQuery

前端 未结 11 1852
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 11:59

I have a set of dynamically generated dropdown boxes on my page. basically I clone them using jQuery. now I want to capture the value selected on each dropdown on change eve

相关标签:
11条回答
  • 2020-12-13 12:37

    To get the value of a drop-down (select) element, just use val().

    $('._someDropDown').live('change', function(e) {
      alert($(this).val());
    });
    

    If you want to the text of the selected option, using this:

    $('._someDropDown').live('change', function(e) {
      alert($('[value=' + $(this).val() + ']', this).text());
    });
    
    0 讨论(0)
  • 2020-12-13 12:41

    This is what you need :)

    $('._someDropDown').live('change', function(e) {
        console.log(e.target.options[e.target.selectedIndex].text);
    });
    

    For new jQuery use on

    $(document).on('change', '._someDropDown', function(e) {
        console.log(this.options[e.target.selectedIndex].text);
    });
    
    0 讨论(0)
  • 2020-12-13 12:44
    $("#citiesList").change(function() {
        alert($("#citiesList option:selected").text());
        alert($("#citiesList option:selected").val());              
    });
    

    citiesList is id of select tag

    0 讨论(0)
  • 2020-12-13 12:44

    In case you want the index of the current selected value.

    $selIndex = $("select#myselectid").prop('selectedIndex'));
    
    0 讨论(0)
  • 2020-12-13 12:47

    You can also use :checked

    $("#myselect option:checked").val(); //to get value
    

    or as said in other answers simply

    $("#myselect").val(); //to get value
    

    and

    $("#myselect option:checked").text(); //to get text
    
    0 讨论(0)
  • 2020-12-13 12:48

    To get the text of the selected option

    $("#your_select :selected").text();
    

    To get the value of the selected option

    $("#your_select").val();
    
    0 讨论(0)
提交回复
热议问题