jQuery 将第一个 <select> 选项设置为选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5460885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:12:33  来源:igfitidea点击:

Setting first <select> option as selected

jqueryselect

提问by Juan Ignacio

I'm trying to set the first of a few to set as selected

我正在尝试将几个中的第一个设置为选定

I have

我有

<select name="id[21]" class="atributosSort">
  <option value="107">1. Sin adaptadores de video (+)</option>
  <option value="108">2. Mini Displayport to DVI (+2)</option>
  <option value="109">3. Mini Displayport to VGA</option>
</select>

I tried

我试过

jQuery(".atributosSort")[0].selectedIndex = 0;

and

jQuery(".atributosSort option:first").attr('selected','selected');

both discussed at How to make first option of <select > selected with jQuery?but when I inspect the DOM, no attribute is added to any

两者都在 How to make first option of <select > selected with jQuery? 中讨论?但是当我检查 DOM 时,没有向任何属性添加任何属性

I'm using jQuery() because I'm in noConflict mode. Could that be the issue?

我使用 jQuery() 因为我处于 noConflict 模式。这可能是问题吗?

When I run both attempts to get the selected value I don't get any error in the Script Console

当我运行两次尝试获取所选值时,脚本控制台中没有任何错误

Another thing that might be relevant is that I'm sorting the options with this

另一件可能相关的事情是我正在用这个对选项进行排序

function sortDropDownListByText() {
// Loop for each select element on the page.
jQuery("select").each(function() {

    // Keep track of the selected option.
    var selectedValue = jQuery(this).val();

    // Sort all the options by text. I could easily sort these by val.
    jQuery(this).html(jQuery("option", jQuery(this)).sort(function(a, b) {
        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    }));

    // Select one option.
    //jQuery(this).val(selectedValue);

});
}

  // Use jQuery via jQuery(...)
 jQuery(document).ready(sortDropDownListByText);

I commented jQuery(this).val(selectedValue); out because I thought that that line was setting some option as selected and then I can't override it but that makes no difference.

我评论了 jQuery(this).val(selectedValue); 因为我认为该行将某些选项设置为选定的,然后我无法覆盖它,但这没有区别。

Any ideas? Thanks

有任何想法吗?谢谢

回答by mattsven

Have you tried:

你有没有尝试过:

jQuery(".atributosSort option:first-child").attr("selected", true);

?

?

回答by Bob

I am using "Select one..." as my first option, with no value defined. I found this works for me:

我使用“选择一个...”作为我的第一个选项,没有定义任何值。我发现这对我有用:

//for elements having classname, get the first option and make it selected
$('.classname').find('option:first').attr('selected','selected');

This is similar to the one @NeXXeuS posted, but will do it for all your select fields.

这与@NeXXeuS 发布的类似,但会为您选择的所有字段执行此操作。

回答by Steve Ardianto

jQuery(document).ready(function () {
    jQuery(".atributosSort")[0].selectedIndex = 0;
});

by Bill the Lizard?

比尔蜥蜴?

is the best answer, it doesnt bug with selected attribute on select.

是最好的答案,它不会在选择时使用选定的属性。

回答by Audee Velasco

for newer version of jQuery:

对于较新版本的 jQuery:

$(".atributosSort").prop("selectedIndex", 0)

回答by ArlanG

Have you tried this code before? I use it for reset my dropdowns on too many projects using jQuery :

你以前试过这个代码吗?我用它来重置我使用 jQuery 的太多项目的下拉列表:

// Use jQuery via jQuery(...)
 $("#yourTarget").val($("#yourTarget option:first").val());

I Hope this could help you.

我希望这可以帮助你。

回答by Roman

Are you doing that when $(document).ready(...)?

你什么时候这样做$(document).ready(...)

Try this:

尝试这个:

jQuery(document).ready(function () {
    jQuery(".atributosSort")[0].selectedIndex = 0;
});

回答by dev4life

Have you tried: (putting parentheses after "sortDropDownListByText" in the "ready" statement)

您是否尝试过:(在“ready”语句中的“sortDropDownListByText”之后加上括号)

// Use jQuery via jQuery(...)
 jQuery(document).ready(sortDropDownListByText(););