用选择器加速这个jQuery代码

时间:2010-12-16 18:08:54

标签: jquery jquery-selectors performance

我为许多对象列表编写了一个过滤器。用户可以通过单选按钮选择他们的选项。

在我的过滤器功能中,我选择两个无线电组/组的已检查单选按钮:

if ( $('#filter input[name="interval"]:checked').val() == 'all' ) {
    if( $('#filter input[name="day"]:checked').val() == 'all' ) {
        var searchSelector = "li";
    } else {
        var searchSelector = "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
    }
} else {
    if( $('#filter input[name="day"]:checked').val() == 'all' ) {
        var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]";
    } else {
        var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]" + "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
    }
}

正如在jQuery Performance Rules中所说的,我应该缓存jQuery对象。通常这种做法总是有效 - 但在这种情况下,我的第二个单选按钮的值总是输出第一个选中值的值。

任何想法,我如何简化这个jQuery代码?

感谢

以下是按钮的HTML:

<form id="filter">
    <fieldset>
        <label>
            <input type="radio" name="interval" value="all">
            all</label>
        <label>
        <label>
            <input type="radio" name="interval" value="hourly">
            hourly</label>
        <label>
            <input type="radio" name="interval" value="daily">
            daily</label>
        <label>
            <input type="radio" name="interval" value="weekly">
            weekly</label>
        <label>
            <input type="radio" name="interval" value="monthly">
            monthly</label>
        <label>
            <input type="radio" name="interval" value="yearly">
            yearly</label>
    </fieldset>
    <fieldset>
        <label>
            <input type="radio" name="day" value="all">
            all</label>
        <label>
            <input type="radio" name="day" value="monday">
            monday</label>
        <label>
            <input type="radio" name="day" value="tuesday">
            tuesday</label>
        <label>
            <input type="radio" name="day" value="wednesday">
            wednesday</label>
        <label>
            <input type="radio" name="day" value="thursday">
            thursday</label>
        <label>
            <input type="radio" name="day" value="friday">
            friday</label>
    </fieldset>
</form>

这就是我的清单:

<li data-day="monday" data-interval="weekly">foo</li>
<li data-day="friday" data-interval="yearly">foo</li>

2 个答案:

答案 0 :(得分:2)

以下应该这样做

var $interval = $('#filter input[name="interval"]'),
    $day = $('#filter input[name="day"]');

    $('#filter input:radio').change(function(){
        var interval = $interval.filter(':checked').val() || 'all',
            day = $day.filter(':checked').val()|| 'all',
            searchSelector = 'li';

            searchSelector += (day != 'all') ? '[data-day=' + day  + ']' : '';
            searchSelector += (interval != 'all') ? '[data-interval=' + interval  + ']' : '';
          }
        /*do what you want with the selector here*/
    });

示例:http://www.jsfiddle.net/gaby/xPMn5/

答案 1 :(得分:1)

为什么不做这样的事情 - 我假设searchSelector稍后在代码中应用于.find(),因为没有定义ul来查看...

// This may also be $("li") -- I was assuming you were using it for a .find()
var $result = $something.find("li");
// This finds "all" the possible results - now to filter we need the values:
// Use || 'all' to default to "all" if there is no "checked" filter
var interval = $('#filter input[name="interval"]:checked').val() || 'all';
var day = $('#filter input[name="day"]:checked').val() || 'all';

// if we need to filter this list at all:
if (day != 'all' || interval != 'all') {
  // store the new result of the filter
  $result = $result.filter(function() {
    // check the .data-day .data-interval attributes on this directly:
    if (day != 'all' && this.data-day != day) return false;
    if (interval != 'all' && this.data-interval != interval) return false;
    return true;
  });
}

我还假设您正在使用此功能以某种方式切换这些<li>的可见性...在第一行中,您可以将.hide()附加到链上,然后在$results.show()处调用{{1}}这段代码的结尾