[英]how to set selected attribute in option for select drop down list
here is the html code 这是HTML代码
<select id="privacy" onChange="run()">
<option value="1" selected="selected" >public</option>
<option value="2">contact</option>
</select>
for value=2,
im selecting contact and this contact is appended in the list but its not showing as selected.below is the jquery code for appending but im not getting where to set this selected atrribute. 对于value=2,
即时通讯录选择联系人并将此通讯录追加到列表中,但未显示为选定。以下是用于追加但无法在何处设置此选定属性的jquery代码。
$('#privacy').append($('<option> ', {
text: postcontact
}));
USe selected
attribute like this way 以这种方式使用selected
属性
$('#privacy').append($('<option> ', {
text: "postcontact",
selected: "selected"
}));
Try to use chaining at this context , 尝试在这种情况下使用链接,
$('#privacy').append($('<option> ', {text: postcontact ,value:2})).val(2);
or try this 或尝试这个
$('#privacy').find("option").removeAttr("selected").end().append($('<option> ', {text: "postcontact" ,value:3,selected: "selected"})).val(3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.