简体   繁体   English

选择元素上的占位符选项,默认选中

[英]Placeholder option on select element with default selected

Bit of a weird one, not sure if this is possible or not. 有点奇怪,不确定这是否可行。

The system I am working on has a sort by select element which has 'Recommended Products' as the selected option on load as this is the order in which they'd like the products to be when a user hits the page, but they also want it to say 'Sort By' be default so that it's obvious this is a sort by element. 我正在使用的系统具有一个按选择排序的元素,该元素在加载时具有“推荐产品”作为选定选项,因为这是用户点击页面时他们希望产品出现的顺序,但他们也希望可以说默认为“排序依据”,因此很明显这是一个按元素排序。 Unfortunately I can't change the core HTML, I can only modify via jQuery/Javascript. 不幸的是,我无法更改核心HTML,只能通过jQuery / Javascript进行修改。

Just wondering if there was a way to do this with jQuery or not, as the method I've tried have interfered with the default select so far. 只是想知道是否有一种使用jQuery的方法,因为到目前为止我尝试过的方法已经干扰了默认选择。 Is it even possible to have a default placeholder as well as another option selected to dictate the load in state? 甚至有可能具有默认的占位符以及选择其他选项来决定状态的负载吗?

Cheers! 干杯!

Current HTML 目前的HTML

<select id="sort-by-select">
  <option value="#" selected="">Recommended Products</option>
  <option value="#">Price (High to Low)</option>
  <option value="#">Price (Low to High)</option>
  <option value="#">Newest In</option>
  <option value="#">Most Popular</option>
</select>

Desired HTML 所需的HTML

<select id="sort-by-select">
  <option value="#" selected="" disabled="disabled">Sort By</option>
  <option value="#" selected="">Recommended Products</option>
  <option value="#">Price (High to Low)</option>
  <option value="#">Price (Low to High)</option>
  <option value="#">Newest In</option>
  <option value="#">Most Popular</option>
</select>

As pointed out in the comments, you can only have one default/selected option. 如评论中所指出的,您只能有一个默认/选定的选项。 What you could do instead is add a label via jQuery, using .insertBefore() : 您可以做的是使用.insertBefore()通过jQuery添加标签:

 $(function() { var $label = $('<label>').text('Sort By:').attr('for', 'sort-by-select'); $label.insertBefore($('#sort-by-select')); }); 
 label { margin-right: 5px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="sort-by-select"> <option value="#" selected="">Recommended Products</option> <option value="#">Price (High to Low)</option> <option value="#">Price (Low to High)</option> <option value="#">Newest In</option> <option value="#">Most Popular</option> </select> 

Alternatively, you could prepend "Sort by" to every option in the select, using .text() : 或者,您可以使用.text()在选择的每个选项前添加“排序依据”:

 $(function() { $('#sort-by-select option').text(function () { return 'Sort by: ' + $(this).text(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="sort-by-select"> <option value="#" selected="">Recommended Products</option> <option value="#">Price (High to Low)</option> <option value="#">Price (Low to High)</option> <option value="#">Newest In</option> <option value="#">Most Popular</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM