Changing the Selected value of Select Menu Jquery Mobile

扶醉桌前 提交于 2019-12-13 15:23:22

问题


I have created a form and have stored the values selected by the user in a database. But now if the user wants to edit his form, i need to reload the form with the previous values.I am using JQUERY MOBILE

I retrieved his previous values from the database but now i am having a problem with loading the values in a select Menu.Can anyone help me ?

I have used the following code :-

     var nameVar = (dataset.item(id)['name']);  // Getting the name from the database
     $('#StateName').val(nameVar);                   // StateName is the id  
     $('#StateName').selectmenu('refresh', true);    // Refreshing the Select Menu

But even after the execution of the above code nothing is reflected in the select Menu Is something Wrong in Code or am i missing something ?


回答1:


Instead of value you need to append the option:

var nameVar = (dataset.item(id)['name']);
$('#StateName').append('<option value="'+nameVar+'" selected="selected">'+nameVar+'</option>');                     
$('#StateName').selectmenu('refresh', true);  

Or if you have the Select Options already and just need to select the option, try this:

var nameVar = (dataset.item(id)['name']);
$('#StateName option[value='+nameVar+']').attr('selected', 'selected');
$('#StateName').selectmenu('refresh', true);

More on Select Menu for jQM 1.0 here:

  • http://jquerymobile.com/demos/1.0/docs/forms/selects/


来源:https://stackoverflow.com/questions/8309886/changing-the-selected-value-of-select-menu-jquery-mobile

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