问题
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