jQuery: set selected value of dropdown list?

后端 未结 3 733
我在风中等你
我在风中等你 2021-02-01 02:47

What\'s wrong with this code?

jQuery

$(document).ready(function() {
    $(\"#routetype\").val(\'quietest\');
)};

HTML

&         


        
相关标签:
3条回答
  • 2021-02-01 02:57

    You need to select jQuery in the dropdown on the left and you have a syntax error because the $(document).ready should end with }); not )}; Check this link.

    0 讨论(0)
  • 2021-02-01 03:01

    You can select dropdown option value by name

    jQuery("#option_id").find("option:contains('Monday')").each(function()
    {
     if( jQuery(this).text() == 'Monday' )
     {
      jQuery(this).attr("selected","selected");
      }
    });
    
    0 讨论(0)
  • 2021-02-01 03:04

    UPDATED ANSWER:

    Old answer, correct method nowadays is to use jQuery's .prop(). IE, element.prop("selected", true)

    OLD ANSWER:

    Use this instead:

    $("#routetype option[value='quietest']").attr("selected", "selected");
    

    Fiddle'd: http://jsfiddle.net/x3UyB/4/

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