【发布时间】:2012-02-24 18:23:01
【问题描述】:
我创建了一个动态选择列表框,如下所示
for (var i in c) {
//alert(c[i].IsDefault);
if(c[i].IsDefault==true){
//alert('found default');
$("#AnsType").append("<option value='" + c[i].ID + "'>" + c[i].Code+ "</option>");
temp = c[i].Code;
}
else{
$("#AnsType").append("<option value='" + c[i].ID + "'>" + c[i].Code+ "</option>");
}
}
刷新了
$('#AnsType').selectmenu('refresh');
我尝试了所有这些方法并尝试在列表中选择第二个元素对我没有用。
$('#AnsType').val(temp.toString());
$('#AnsType').get(3).selectedIndex = 3;
$('select#AnsType').val('3');
$("#AnsType option[text='3']").attr("selected","selected") ;
$("#mydropdownlist").attr('selectedIndex', 1);
$('#AnsType').val(3);
$("#AnsType").attr('selectedIndex', 2);
$("#AnsType").val(c[parseInt(temp)].Code);
$("select#AnsType option[selected]").removeAttr("selected");
$("select#AnsType option[value='"+temp+"']").attr("selected", "selected");
这就是我的列表框的创建方式
<select id="AnsType" name="AnsType">
<option value="1">Obokad</option>
<option value="3">Egen bokning</option>
</select>
任何帮助表示赞赏,并提前感谢您的帮助..
【问题讨论】:
-
你想做什么??
-
我想选择列表中的第二个元素。
-
在要保持选中状态的选项中添加 selected="selected" 属性。
-
$('#AnsType').get(3).selectedIndex = 3;- ID 选择器应该只返回一个元素(因为 ID 应该是唯一的!),所以我不确定您期望get(3)返回什么。
标签: javascript jquery jquery-selectors jquery-mobile