简体   繁体   English

如何在选择下拉列表的选项中设置所选属性

[英]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.

相关问题 需要从下拉列表列表中选择具有特定属性值的下拉列表元素,然后设置其选定值 - Need to select a drop down list element with a particular attribute value from a list of drop down lists and set its selected value 如何使用 jQuery、JavaScript 和 HTML 动态设置下拉列表的选定选项? - How do I dynamically set the selected option of a drop-down list using jQuery, JavaScript and HTML? 如何使用D3.js从下拉列表中设置最后一个选项 - How to set the last option from a drop-down list as selected using D3.js 使用jQuery将“ selected”属性添加到下拉选项中 - Add the “selected” attribute to a drop down option with jQuery 计算选择下拉列表中每个选项的选择次数 - Couting how many times each option is selected in Select drop down 如何根据下拉菜单中的所选选项设置隐藏字段的值? - How to set value for hidden field based on selected option in drop down? 如何在当前时间的下拉菜单中设置自动选择的选项? - How to set auto selected option in drop-down with current time? 选择下拉“选定”选项不会突出显示 - Select drop down “selected ”option not getting highlighted 如何将json数组元素映射到下拉列表中选择的选项? - How to map json array elements to option selected in drop down list? 如何设置选择标签选项的下拉列表的高度和宽度 - How to set the height and width of select tag option's drop down list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM