Set select option 'selected', by value

后端 未结 28 2776
攒了一身酷
攒了一身酷 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:28

    There seems to be an issue with select drop down controls not dynamically changing when the controls are dynamically created instead of being in a static HTML page.

    In jQuery this solution worked for me.

    $('#editAddMake').val(result.data.make_id);
    $('#editAddMake').selectmenu('refresh');
    

    Just as an addendum the first line of code without the second line, did actually work transparently in that, retrieving the selected index was correct after setting the index and if you actually clicked the control it would show the correct item but this didn't reflect in the top label of the control.

    Hope this helps.

提交回复
热议问题