我在第2页有一个选择菜单,它将填充来自DB的动态值。每次当我转到页面时,我都想选择之前选择的值。我能够获得先前选择的值,但是当我尝试将先前选择的值设置为默认值时,它不起作用。谁能告诉我我做了什么错?我的代码如下,
$(document).on("pageshow","#saveToDBPage",function(event){
var selecedvalue = $('#select-choice-1 :selected').val();
alert(selecedvalue); // This is Previously selected value........
var db = window.sqlitePlugin.openDatabase({name: "MYDB"});
db.transaction(function (tx) {
tx.executeSql("select distinct Category from Locationlog;", [], function (tx, res) {
$("#select-choice-1").empty();
var optionheading = '<option value="Select Category">Select Category</option>';
$("#select-choice-1").append(optionheading);
for (var i = 0; i < res.rows.length; i++)
{
var opt = '<option value="';
opt += res.rows.item(i).Category;
opt += '">';
opt += res.rows.item(i).Category;
opt += '</option>';
$("#select-choice-1").append(opt).selectmenu('refresh');
$("select#select-choice-1").val(selecedvalue); // IT IS NOT WOKING....
}
});
});
});
答案 0 :(得分:0)
试试这个:
$('#select-choice-1 option[value='+selecedvalue+']').attr("selected", "selected");
或
$('#select-choice-1').val(selecedvalue);
更新:以编程方式操作jQuery Mobile中的选择字段等元素时,一旦进行了相关选择,就需要刷新元素以便更新用户界面:
$('#select-choice-1').val(selecedvalue).attr('selected', true).siblings('option').removeAttr('selected');
$('#select-choice-1').selectmenu("refresh", true);