Set select option 'selected', by value

后端 未结 28 2340
攒了一身酷
攒了一身酷 2020-11-22 10:40

I have a select field with some options in it. Now I need to select one of those options with jQuery. But how can I do that when I only know the

28条回答
  •  清酒与你
    2020-11-22 11:23

    An issue I ran into when the value is an ID and the text is a code. You cannot set the value using the code but you don't have direct access to the ID.

    var value;
    
    $("#selectorId > option").each(function () {
      if ("SOMECODE" === $(this).text()) {
        value = $(this).val();
      }
    });
    
    //Do work here
    

提交回复
热议问题