JQuery - how to select dropdown item based on value

前端 未结 13 2028
[愿得一人]
[愿得一人] 2020-12-04 17:18

I want set a dropdown(select) to be change based on the value of the entries.

I have


                        
    
提交评论

  • 2020-12-04 17:57

    $('select#myselect option[value="ab"]').

    0 讨论(0)
  • 2020-12-04 17:57

    You can select dropdown option value by name

    // deom
    jQuery("#option_id").find("option:contains('Monday')").each(function()
    {
     if( jQuery(this).text() == 'Monday' )
     {
      jQuery(this).attr("selected","selected");
      }
    
    });
    
    0 讨论(0)
  • 2020-12-04 17:58

    In your case $("#mySelect").val("fg") :)

    0 讨论(0)
  • 2020-12-04 17:59

    You should use

    $('#dropdownid').val('selectedvalue');
    

    Here's an example:

    $('#dropdownid').val('selectedvalue');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id='dropdownid'>
        <option value=''>- Please choose -</option>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='selectedvalue'>There we go!</option>
        <option value='3'>3</option>
        <option value='4'>4</option>
        <option value='5'>5</option>
    </select>

    0 讨论(0)
  • 2020-12-04 18:06
    $('#mySelect').val('fg');...........
    
    0 讨论(0)
  • 提交回复
    热议问题