按值属性排序选择选项使用jQuery

时间:2013-12-19 23:17:53

标签: javascript jquery json handlebars.js

嗯,标题说明了一切。我正在做的是创建一个特色产品模块。使用JSON填充下拉列表的大小,我使用把手来渲染html。我无法控制JSON文件。我尝试按选项标签中的实际文本对选项值进行排序,但我意识到之后选项值是错误的。所以现在我试图通过它们的值属性对选项进行排序,但还没有想出来。我想做这样的事情:

var selectList = $('#featuredSelectField option');
var newList = [];
var theNewNew = [];
for(var i=0; i<selectList.length; i++){
    newList[i]=(selectList[i].value);    
}
newList.sort();
for(var i=0; i<newList.length; i++){
    theNewNew[i] = $('#featuredSelectField option[value="'+newList[i]+'"]');
    selectList[i] = theNewNew[i];
}

这是html:

<select id="featuredSelectField" name="addid7617843" size="1">
    <option value="" data-value="">Select an option</option>
    <option value="10493640" data-value="10493640" data-qty="30" data-mxqty="30">Size 5.5 - $111.99</option>
    <option value="10493639" data-value="10493639" data-qty="120" data-mxqty="50">Size 5 - $111.99</option>
    <option style="display: none;" disabled="disabled" value="10792504" data-value="10792504" data-qty="0" data-mxqty="0">Size 10 - $111.99 Sold Out</option>
    <option value="10493644" data-value="10493644" data-qty="138" data-mxqty="50">Size 7 - $111.99</option>
    <option value="10493642" data-value="10493642" data-qty="22" data-mxqty="22">Size 6.5 - $111.99</option>                
    <option value="10493641" data-value="10493641" data-qty="57" data-mxqty="50">Size 6 - $111.99</option>              
    <option value="10493648" data-value="10493648" data-qty="99" data-mxqty="50">Size 9 - $111.99</option>      
    <option value="10493647" data-value="10493647" data-qty="28" data-mxqty="28">Size 8.5 - $111.99</option>                
    <option value="10493646" data-value="10493646" data-qty="74" data-mxqty="50">Size 8 - $111.99</option>                  
    <option value="11288830" data-value="11288830" data-qty="1" data-mxqty="1">Size 4.5 - $111.99</option>
    <option value="10493645" data-value="10493645" data-qty="51" data-mxqty="50">Size 7.5 - $111.99</option>                    
    <option value="10792503" data-value="10792503" data-qty="5" data-mxqty="5">Size 9.5 - $111.99</option>
    <option value="11288831" data-value="11288831" data-qty="6" data-mxqty="6">Size 4 - $111.99</option>
</select>

3 个答案:

答案 0 :(得分:34)

var selectList = $('#featuredSelectField option');

selectList.sort(function(a,b){
    a = a.value;
    b = b.value;

    return a-b;
});

$('#featuredSelectField').html(selectList);

在这里,您可以找到一个演示并将结果与​​原始结果进行比较: http://jsfiddle.net/74c2M/3/

在这里您可以找到正确的代码: http://jsfiddle.net/74c2M/4/

祝你好运!

答案 1 :(得分:1)

$(function() {
  // choose target dropdown
  var select = $('select');
  select.html(select.find('option').sort(function(x, y) {
    // to change to descending order switch "<" for ">"
    return $(x).text() > $(y).text() ? 1 : -1;
  }));

  // select default item after sorting (first item)
  // $('select').get(0).selectedIndex = 0;
});

答案 2 :(得分:0)

app:menu
const sort = (arr, p, o = "asc") => arr.sort((a, b) => {
  if (o !== "asc")[a, b] = [b, a];
  const isNum = typeof b[p] === "number";
  return (isNum ? Number(a[p]) - b[p] : String(a[p]).localeCompare(b[p]));
});


$.fn.sortChildren = function(op) {
  op = $.extend({
    by: "textContent",
    order: "asc"
  }, op);
  return this.each(function() {
    const i = $(this).prop("selectedIndex");
    $(this).html(sort($(this).children(), op.by, op.order)).prop({selectedIndex: i});
  });
};


// 1. example: sorting by value, order "asc" (default)
$("#test_1").sortChildren({by: "value"});

// 2. example: sorting by textContent (default), order "desc"
$("#test_2").sortChildren({order: "desc"});