【问题标题】:Setting first <select> option as selected将第一个 <select> 选项设置为选中
【发布时间】:2025-11-25 10:20:14
【问题描述】:

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

我有

<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>

我试过了

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

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

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

我正在使用 jQuery(),因为我处于 noConflict 模式。会不会是这个问题?

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

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

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);

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

有什么想法吗? 谢谢

【问题讨论】:

    标签: jquery select


    【解决方案1】:

    你试过了吗:

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

    ?

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:
        jQuery(document).ready(function () {
            jQuery(".atributosSort")[0].selectedIndex = 0;
        });
        

        蜥蜴比尔 ♦

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

        【讨论】:

          【解决方案4】:

          对于较新版本的 jQuery:

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

          【讨论】:

            【解决方案5】:

            你试过了吗:(在“ready”语句中的“sortDropDownListByText”后面加上括号)

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

            【讨论】:

              【解决方案6】:

              $(document).ready(...) 时你这样做吗?

              试试这个:

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

              【讨论】:

              • 试试这个:jQuery(documnet).ready(function(){jQuery(".atributosSort")[0].selectedIndex = 0;});
              【解决方案7】:

              您以前尝试过此代码吗?我用它来重置我在太多使用 jQuery 的项目上的下拉列表:

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

              我希望这可以帮助你。

              【讨论】:

                最近更新 更多