How to select an option in a jQuery ui selectmenu dynamically?

后端 未结 6 1299
别跟我提以往
别跟我提以往 2021-02-18 21:44

After changing a menu from a regular select to a jQuery selectmenu, I can no longer select options in it programatically. Is there a way to do this?

The code to select

6条回答
  •  暖寄归人
    2021-02-18 22:40

    Please note you must use indexes (not values) to select the option using this method.

    For instance, given this select.

    
    

    In this case the expression $('#ListId').selectmenu("value", "value-two"); wouldn't return any result. Instead of that, we have to use $('#ListId').selectmenu("value", "2");. In other words, we have to use the position/index of the element inside the select. Not the value!

    Finding the index is easy though. You can use the following line to achieve it.

    var index = $('#ListID option[value="value-two"]').index();
    $('#ListId').selectmenu("value", index);
    

提交回复
热议问题