【问题标题】:Set selected using jQuery使用 jQuery 设置选择
【发布时间】:2011-06-02 03:15:00
【问题描述】:

我正在尝试更改此列表中的选定选项。它只适用于某些人而不适用于其他人?

selectedVal 将是 Kelly Green、Navy 等...

 var selectedVal = $(this).text();

 $("#product-variants-option-0 option[text=" + selectedVal+"]").attr("selected","selected") ;

这是选择列表:

<select class="single-option-selector" id="product-variants-option-0">
<option value="Gunmetal Heather">Gunmetal Heather</option>
<option value="Kelly Green">Kelly Green</option>
<option value="Navy">Navy</option>
</select>

【问题讨论】:

    标签: jquery select selector options attr


    【解决方案1】:

    在选择器中使用值属性而不是文本。即:

    $("#product-variants-option-0 option[value=" + selectedVal+"]").attr("selected","selected") ;
    

    【讨论】:

    • 不敢相信我错过了...非常感谢!完美!
    【解决方案2】:

    有一种更简单的方法来设置 &lt;select&gt; 元素的选定选项。

    只需针对&lt;select&gt; 本身调用jQuery's .val() method

    $("#product-variants-option-0").val( selectedVal );
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 2014-11-20
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多