简体   繁体   English

如何在文档加载时使用jquery在选择框中选择特定项目

[英]How do i make the particular item selected in select box using jquery on document load

iam having the select box 我有选择框

<select id ="myselect">
<option value="AA">AA</option>
<option value="AB">AB</option>
<option value="AC">AC</option>
<option value="AD">AD</option>
</select>

so on document load I want to Make item "AC" in the select box as the default selected option on loading the document 所以在文档加载时我想在选择框中将项目“AC”作为加载文档时的默认选择选项

$('#myselect').children('option').text('AC').attr('selected', 'selected');

so with this statement written it is making my select box to fill with values 'AC' completely for my select box. 所以写了这个声明,它使我的选择框完全为我的选择框填充值'AC'。 How do i correct that and i tried in using the if statement comparing the value for 'AC'.And i dont have the "Value" for option as number 我如何纠正这一点,我尝试使用if语句比较'AC'的值。我没有选项的“值”作为数字

It should be do-able with: 应该可以做到:

$('option[value=AC]').attr('selected','selected');

Demo at: jsbin . 演示: jsbin

You can use .val() to set the value of the select. 您可以使用.val()来设置select的值。 This will select the option with the specified value. 这将选择具有指定值的选项。

$("#myselect").val('AC');

Example here: http://jsfiddle.net/mtd6z/ 示例: http//jsfiddle.net/mtd6z/

你可能想要

 $('#myselect').children('option').filter(function(){ return $(this).text() == 'AC' }).attr('selected', 'selected');

I suggest not using Javascript for this (unless some very important reason ) - 我建议不要使用Javascript(除非一些非常重要的原因) -

It can be simply done this way 它可以这样简单地完成

<select id ="myselect">
<option value="AA">AA</option>
<option value="AB">AB</option>
<option value="AC" selected="selected">AC</option>
<option value="AD">AD</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Jquery选择特定的列表框项目? - How to select the particular list box item using Jquery? 如何在选择框的第一项被选中时使颜色变灰 - How do I make the color gray when the first item of the select box is selected 选择选择框中的任何项目时,如何在jQuery中触发事件? - How do I trigger a event in jQuery when any item in a select box is selected? 如何使用jquery更改标签的文本,使其与从选择框中选择的值相同? - How do change the text of a label using jquery to make it the same as the value selected from a select box? 使用jQuery设置选择框的特定选项选择的属性 - Set attribute selected of particular option of select box using jquery 如何使用jQuery从选择框中清除所选值? - How do I clear the selected value from a select box using jQuery? 在使用JavaScript或JQuery选择HTML中的选定项目之后,如何禁用项目? - How do I disable items after the selected item in an HTML select using JavaScript or JQuery? jQuery根据不同选择框的选择值使选择框在页面加载时可见 - JQuery make select box visible on page load based on selected value of a different select box 如何根据“选择”框中的项目选择提供所选属性? - How do I give the selected properties according to the item selection in the Select box? 使用jQuery在选择框中获取以前选择的项目 - Getting previously selected item in select box with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM