jquery cant设置选定的选项动态创建下拉列表

时间:2015-10-15 17:00:10

标签: jquery drop-down-menu html-select

我在移动应用中填充了一组来自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}}

1 个答案:

答案 0 :(得分:1)

如果选择选项生成正确,那么它应该有效:

   complete: function() {
    var selectedGroup = localStorage.getItem('selectedGroupName');
    $('#select_group_drop option').each(function(){              
             if($(this).val()==selectedGroup ){
                $(this).attr('selected','selected');
            }
        });
    }