将第一个选项设置为选中

时间:2021-06-29 21:22:23

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 (+$45)</option>
  <option value="108">2. Mini Displayport to DVI (+$112)</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

在如何使用jQuery选择

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

有任何想法吗?谢谢

7 个解决方案

#1


24  

Have you tried:

你有没有尝试过:

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

?

#2


2  

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类似,但会对所有选择字段执行此操作。

#3


1  

for newer version of jQuery:

对于更新版本的jQuery:

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

#4


0  

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

您是否尝试过:(在“ready”语句中将“sortDropDownListByText”后面的括号放入)

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

#5


0  

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

你在$(文件).ready(...)时这样做吗?

Try this:

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

#6


0  

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.

是最好的答案,它不会选择选择属性的错误。

#7


0  

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.

我希望这可以帮到你。

#1


24  

Have you tried:

你有没有尝试过:

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

?

#2


2  

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类似,但会对所有选择字段执行此操作。

#3


1  

for newer version of jQuery:

对于更新版本的jQuery:

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

#4


0  

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

您是否尝试过:(在“ready”语句中将“sortDropDownListByText”后面的括号放入)

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

#5


0  

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

你在$(文件).ready(...)时这样做吗?

Try this:

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

#6


0  

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.

是最好的答案,它不会选择选择属性的错误。

#7


0  

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.

我希望这可以帮到你。