我在移动应用中填充了一组来自Web服务的选项。我想为返回的用户设置选择,但是无法在下拉列表中将其设置为选项。
HTML
function get_teams(){
var servicePath = "http://domain.com/server/";
function getGroups()
{
$("#select_group_drop").html("");
$.ajax({
url: servicePath + "manage_teams.php",
data:
{
action: "getAllTeams",
email: localStorage.getItem("email")
},
dataType: "json",
method: "post",
success: function(data)
{
$("#select_group_drop").html(data.results);
},
complete: function() {
var selectedGroup = localStorage.getItem('selectedGroupName');
$("#select_group_drop option[text="+selectedGroup+"]").attr("selected","selected");
}
});
}
的Javascript
{{1}}
答案 0 :(得分:1)
如果选择选项生成正确,那么它应该有效:
complete: function() {
var selectedGroup = localStorage.getItem('selectedGroupName');
$('#select_group_drop option').each(function(){
if($(this).val()==selectedGroup ){
$(this).attr('selected','selected');
}
});
}