jquery mobile - set select/option values

◇◆丶佛笑我妖孽 提交于 2019-12-03 05:49:36
Ben

When programmatically manipulating elements like select fields in jQuery Mobile, once you've made the relevant selection, you need to refresh the element so that the user interface is updated. Here's an example snippet which sets a value in a select field, and then updates it:

// Grab a select field
var el = $('#YOUR_SELECT_FIELD');

// Select the relevant option, de-select any others
el.val('some value').attr('selected', true).siblings('option').removeAttr('selected');

// jQM refresh
el.selectmenu("refresh", true);

So it's that last line I suspect you need.

In some cases you might need to wrap your function to execute on document.ready and initialize the selectmenu. Here is my solution using Ben Poole's example:

$(document).ready(function(){
// Grab a select field
var el = $('#YOUR_SELECT_FIELD');

// Select the relevant option, de-select any others
el.val('some value').attr('selected', true).siblings('option').removeAttr('selected');

// Initialize the selectmenu
el.selectmenu();

// jQM refresh
el.selectmenu("refresh", true);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!