jQuery - setting the selected value of a select control via its text description

前端 未结 22 1409
夕颜
夕颜 2020-11-22 09:16

I have a select control, and in a javascript variable I have a text string.

Using jQuery I want to set the selected element of the select control to be the item with

22条回答
  •  借酒劲吻你
    2020-11-22 09:41

    I know this is an old post, but I couldn't get it to select by text using jQuery 1.10.3 and the solutions above. I ended up using the following code (variation of spoulson's solution):

          var textToSelect = "Hello World";
    
          $("#myDropDown option").each(function (a, b) {
                if ($(this).html() == textToSelect ) $(this).attr("selected", "selected");
            });
    

    Hope it helps someone.

提交回复
热议问题