基于值的div内部的jQuery排序复选框 [英] jQuery sort checkboxes inside div's based on value

查看:66
本文介绍了基于值的div内部的jQuery排序复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Wordpress中使用插件生成了几个复选框。
我没有选项对这些复选框进行排序,所以我希望根据它们的值对它们进行排序,最高的是第一个。

I have a couple of checkboxes being generated by a plugin in Wordpress. I don't have the option to sort these checkboxes so I would like to have them sorted based on their value, with the highest first.

HTML生成如下所示:

the HTML generated looks like this:

<div class="wpgmza_filter_container" id="wpgmza_filter_container_5">
        <div class="wpgmza_cat_checkbox_holder wpgmza_cat_checkbox_5">
            <div class="wpgmza_cat_checkbox_item_holder_first">
                <input type="checkbox" class="wpgmza_checkbox form-control" id="wpgmza_cat_checkbox_0" name="wpgmza_cat_checkbox" mid="5" value="0">
                <div>
                    <label for="wpgmza_cat_checkbox_0">All</label>
                </div>
            </div>
            <div class="wpgmza_cat_checkbox_item_holder">
                <input type="checkbox" class="wpgmza_checkbox form-control" id="wpgmza_cat_checkbox_1" name="wpgmza_cat_checkbox" mid="5" value="1">
                <div>
                    <label for="wpgmza_cat_checkbox_1">Airco</label>
                </div>
            </div>
            <div class="wpgmza_cat_checkbox_item_holder">
                <input type="checkbox" class="wpgmza_checkbox form-control" id="wpgmza_cat_checkbox_2" name="wpgmza_cat_checkbox" mid="5" value="2">
                <div>
                    <label for="wpgmza_cat_checkbox_2">Vent</label>
                </div>
            </div>
        </div>
    </div>

我尝试了以下代码:

function sortByID(a, b)
{
return (a.children("input").val() > b.children("input").val()) ? 1 : -1;
}

var container = jQuery("div.wpgmza_filter_container");
container.children("div").sort(sortByID).appendTo(container);


推荐答案

排序逻辑的问题在于你是比较字符串,你还要对 .wpgmza_filter_container 的子元素进行排序,其中只有一个。您需要对 .wpgmza_cat_checkbox_holder 的子项进行排序。您还在排序逻辑中使用了无效的双引号字符。试试这个:

The issue with your sort logic is that you're comparing strings, and you're also sorting the children of .wpgmza_filter_container, of which there is only one. You need to instead sort the children of .wpgmza_cat_checkbox_holder. You are also using an invalid double quote character in your sort logic. Try this:

function sortByValue(a, b) {
  var aVal = parseInt($(a).find('input').val(), 10),
    bVal = parseInt($(b).find('input').val(), 10);
  return aVal < bVal;
}

var $container = $("div.wpgmza_cat_checkbox_holder");
$container.children("div").sort(sortByValue).appendTo($container);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wpgmza_filter_container" id="wpgmza_filter_container_5">
  <div class="wpgmza_cat_checkbox_holder wpgmza_cat_checkbox_5">
    <div class="wpgmza_cat_checkbox_item_holder_first">
      <input type="checkbox" class="wpgmza_checkbox form-control" id="wpgmza_cat_checkbox_0" name="wpgmza_cat_checkbox" mid="5" value="0">
      <div>
        <label for="wpgmza_cat_checkbox_0">All</label>
      </div>
    </div>
    <div class="wpgmza_cat_checkbox_item_holder">
      <input type="checkbox" class="wpgmza_checkbox form-control" id="wpgmza_cat_checkbox_1" name="wpgmza_cat_checkbox" mid="5" value="1">
      <div>
        <label for="wpgmza_cat_checkbox_1">Airco</label>
      </div>
    </div>
    <div class="wpgmza_cat_checkbox_item_holder">
      <input type="checkbox" class="wpgmza_checkbox form-control" id="wpgmza_cat_checkbox_2" name="wpgmza_cat_checkbox" mid="5" value="2">
      <div>
        <label for="wpgmza_cat_checkbox_2">Vent</label>
      </div>
    </div>
  </div>
</div>

如果你想要要确保全部选择​​保留在列表中的第一位,您可以使用从该div中排除过滤器逻辑:不

If you want to ensure that the 'All' selection remains first in the list, you can exclude the filter logic from that div using :not:

$container.children('div:not(".wpgmza_cat_checkbox_item_holder_first")').sort(sortByValue).appendTo($container);

这篇关于基于值的div内部的jQuery排序复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆