Selecting option by text content with jQuery

后端 未结 3 1047
不思量自难忘°
不思量自难忘° 2020-12-25 09:40

I want to set a dropdown box to whatever has been passed through a querystring using jquery.

How do I add the selected attribute to an option such that the \"TEXT\"

3条回答
  •  天命终不由人
    2020-12-25 09:56

    Replace this:

    var cat = $.jqURL.get('category');
    var $dd = $('#cbCategory');
    var $options = $('option', $dd);
    $options.each(function() {
    if ($(this).text() == cat)
        $(this).select(); // This is where my problem is
    });
    

    With this:

    $('#cbCategory').val(cat);
    

    Calling val() on a select list will automatically select the option with that value, if any.

提交回复
热议问题