[英]How do I dynamically set the selected option of a drop-down list using jQuery, JavaScript and HTML?
[英]jquery cant set selected option dynamically created drop down list
我已经从移动应用程序中的Web服务中填充了一组选项。我想为回头用户设置为selected,但是无法在下拉列表中将selected设置为选项。
的HTML
<select id="select_group_drop">
<option >[Select]</option>
</select>
Java脚本
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");
}
});
}
如果选择选项生成正确,则它应该可以工作:
complete: function() {
var selectedGroup = localStorage.getItem('selectedGroupName');
$('#select_group_drop option').each(function(){
if($(this).val()==selectedGroup ){
$(this).attr('selected','selected');
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.