【问题标题】:Select the second value of selecbox选择选择框的第二个值
【发布时间】:2013-10-08 15:55:40
【问题描述】:

是否可以使用 jquery、css 或其他方式选择第二个选项? 我想过使用css nth-child,但不知道如何在实践中做到这一点。 有什么建议么? :)

【问题讨论】:

    标签: html option selected


    【解决方案1】:

    你可以试试这个(使用jQuery

    $(function(){
        $('select option:eq(1)').prop('selected', 1);
    });
    

    DEMO.

    【讨论】:

      【解决方案2】:

      $('#select-id option:nth-child(2)').prop('selected', true);

      【讨论】:

        【解决方案3】:

        我假设默认情况下第一个选项被选中。

        使用 Jquery:

        $('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected');
        

        【讨论】:

          【解决方案4】:
          <option>1</option>
          <option selected>2</option>
          

          【讨论】:

            【解决方案5】:

            您是否尝试将第二个选项设置为“已选择”?

            <select name='s'>
                <option value='1'>1</option>
                <option value='2' selected='selected'>2</option>
            </select>
            

            【讨论】:

              【解决方案6】:

              嗯。到目前为止,建议对我不起作用。这是我正在使用的完整布局:

              jQuery('.priceselector  option:nth-child(2)').prop('selected', true);
              
              <select name="super_attribute[145]" id="attribute145" style="padding:6px;"
              class="priceselector required-entry super-attribute-select">
              
              <option value="">Choose an Option...</option>
              <option value="14" price="1500">20" (w) x 24" (h) Framed  +$1500.00</option> --This needs to bee selected by default
              <option value="15" price="1200">20" (w) x 24" (h) Unframed  +$1200.00</option>
              <option value="18" price="2500">30" (w) x 40" (h) Framed +$2500.00</option>
              <option value="17" price="2000">30" (w) x 40" (h) Unframed +$2000.00</option>
              <option value="16" price="300">8" (w) x 10" (h) Unframed +$300.00</option>
              </select>
              

              【讨论】:

                【解决方案7】:

                将选择框的值更改为由其中一个选项定义的值(并根据需要触发更改事件):

                <select>
                   <option>foo</option>
                   <option selected="selected">bar</option>
                </select>
                
                $("select").val("foo").change();
                

                或通过值或顺序手动选择选项值:

                // deselect the previously selected value first
                $("select option").prop("selected",false);
                
                $("select option").eq(2).prop("selected",true);
                $("select option[value=foo]").prop("selected",true);
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-04-27
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-06-03
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多