Get current value selected in dropdown using jQuery

前端 未结 11 1853
被撕碎了的回忆
被撕碎了的回忆 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:55

    Check it Out-->

    For getting text

    $("#selme").change(function(){
     $(this[this.selectedIndex]).text();
    });
    

    For getting value

    $("#selme").change(function(){
     $(this[this.selectedIndex]).val();
    });
    
    0 讨论(0)
  • 2020-12-13 12:56

    You can try:

    $("._someDropDown").val();
    
    0 讨论(0)
  • 2020-12-13 12:59

    The options discussed above won't work because they are not part of the CSS specification (it is jQuery extension). Having spent 2-3 days digging around for information, I found that the only way to select the Text of the selected option from the drop down is:

    { $("select", id:"Some_ID").find("option[selected='selected']")}
    

    Refer to additional notes below: Because :selected is a jQuery extension and not part of the CSS specification, queries using :selected cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :selected to select elements, first select the elements using a pure CSS selector, then use .filter(":selected"). (copied from: http://api.jquery.com/selected-selector/)

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

    This is actually more efficient and has better readability in my opinion if you want to access your select with this or another variable

    $('#select').find('option:selected')
    

    In fact if I remember correctly phpStorm will attempt to auto correct the other method.

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

    try this...

    $("#yourdropdownid option:selected").val();
    
    0 讨论(0)
提交回复
热议问题