使用选择器加速此 jQuery 代码

发布于 2024-10-07 15:24:34 字数 2889 浏览 2 评论 0原文

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

在我的过滤器功能中,我选择两个单选组/集的选中单选按钮:

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 性能规则 我应该缓存 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>

i wrote a filter for a list of many objects. Users can choose their options in via radio-buttons.

Within my filter functionality i select the checked radio buttons of both radio groups/sets:

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() + "]";
    }
}

As it's said in the jQuery Performance Rules i should cache jQuery objects. Usually this practice always works — but in this case the values of my second radio buttons always outputs the value of the first checked value.

Any ideas, how i can simplify this jQuery code?

THANKS

Here is the HTML for the buttons:

<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>

And this is what my list looks like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-10-14 15:24:34

以下应该执行此

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/

The following should do it

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*/
    });

example: http://www.jsfiddle.net/gaby/xPMn5/

醉生梦死 2024-10-14 15:24:34

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

// 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;
  });
}

我还假设您正在使用它以某种方式切换这些

  • 的可见性...在第一行中,您可以附加 . hide() 到链并在此代码块的末尾调用 $results.show()
  • Why not do something like this -- I am assuming that the searchSelector is being applied to a .find() later in the code as there is no ul defined to look within...

    // 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;
      });
    }
    

    I am also assuming you are using this to toggle the visibility of these <li> somehow... In the first line you could attach .hide() to the chain and call $results.show() at the end of this block of code

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文