jQuery get specific option tag text

后端 未结 22 1652
-上瘾入骨i
-上瘾入骨i 2020-11-22 12:32

All right, say I have this:


                        
    
提交评论

  • 2020-11-22 12:58

    You can get one of following ways

    $("#list").find('option').filter('[value=2]').text()
    
    $("#list").find('option[value=2]').text()
    
    $("#list").children('option[value=2]').text()
    
    $("#list option[value='2']").text()
    

    $(function(){    
        
        console.log($("#list").find('option').filter('[value=2]').text());
        console.log($("#list").find('option[value=2]').text());
        console.log($("#list").children('option[value=2]').text());
        console.log($("#list option[value='2']").text());
        
    });
    <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <select id='list'>
        <option value='1'>Option A</option>
        <option value='2'>Option B</option>
        <option value='3'>Option C</option>
    </select>

    0 讨论(0)
  • 2020-11-22 13:03
    1. If there is only one select tag in on the page then you can specify select inside of id 'list'

      jQuery("select option[value=2]").text();
      
    2. To get selected text

      jQuery("select option:selected").text();
      
    0 讨论(0)
  • 2020-11-22 13:03

    I wanted to get the selected label. This worked for me in jQuery 1.5.1.

    $("#list :selected").text();
    
    0 讨论(0)
  • 2020-11-22 13:04
    $(this).children(":selected").text()
    
    0 讨论(0)
  • 2020-11-22 13:06
    $("#list [value='2']").text();
    

    leave a space after the id selector.

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