jQuery get selected option value (not the text, but the attribute 'value')

后端 未结 14 1994
长情又很酷
长情又很酷 2020-11-29 18:13

Okay, I have this code:


                        
    
提交评论

  • 2020-11-29 18:21
    $('#selectorID').val();
    

    OR

    $('select[name=selector]').val();
    

    OR

    $('.class_nam').val();
    
    0 讨论(0)
  • 2020-11-29 18:21
     $(document ).ready(function() {
        $('select[name=selectorname]').change(function() { 
         alert($(this).val());});
     });
    
    0 讨论(0)
  • 2020-11-29 18:25

    I just wanted to share my experience

    For me,

    $('#selectorId').val()

    returned null.

    I had to use

    $('#selectorId option:selected').val()

    0 讨论(0)
  • 2020-11-29 18:26

    you can use jquery as follows

    SCRIPT

      $('#IDOfyourdropdown').change(function(){
        alert($(this).val());
      });
    

    FIDDLE is here

    0 讨论(0)
  • 2020-11-29 18:32
    $('select').change(function() {
        console.log($(this).val())
    });​
    

    jsFiddle example

    .val() will get the value.

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