如果对象类不包含值,jquery 隐藏对象

发布于 2024-09-16 14:07:48 字数 2215 浏览 11 评论 0原文

标题可能有点令人困惑,基本上我正在尝试编写一个脚本,它将从下拉表单中获取选定的值,并隐藏 li 的类,如果它的类不包含下拉表单中的值..如果那样的话有道理吗??每个 li 有多个类..

这是我到目前为止所得到的(抱歉,如果它很粗糙):

FORM:

<form action="" name="filter" id="filter" method="post">
        <select name="bedrooms">
          <option value="">- Select Bedrooms -</option>
          <option value="bed-1">1</option>
          <option value="bed-2">2</option>
          <option value="bed-3">3</option>
          <option value="bed-4">4</option>
          <option value="bed-5">5</option>
        </select>
        <select name="bathrooms">
          <option value="">- Select Bathrooms -</option>
          <option value="bath-1">1</option>
          <option value="bath-2">2</option>
          <option value="bath-3">3</option>
          <option value="bath-4">4</option>
          <option value="bath-5">5</option>
        </select>
        <select name="frontage">
          <option value="">- Select Frontage Size -</option>
          <option value="frontage-100">100</option>
          <option value="frontage-200">200</option>
          <option value="frontage-300">300</option>
          <option value="frontage-400">400</option>
          <option value="frontage-500">500</option>
        </select>
        <input type="submit" name="filter-submit" id="filter-submit" value="Go" />
      </form>

JQuery:

$("#filter-submit").click(function() {

          var foo = [];
          $("#filter :selected").each(function(i, value){
            foo[i] = $(value).val();
          });
          if (foo) {
            $.each(foo, function(index, value){
              if (value) {
                //hide other objects based on "value"
                //$("#portfolio li").hasClass();
            };
            });
          };

        return false;
        });

好的,所以我陷入困境的是如何隐藏所有没有的“#portfolio li”输出为“值”的类。我想我可以使用 hasClass 但不知道如何扭转它..如果这有意义吗?任何帮助将不胜感激:)

The title might be a bit confusing, basically I'm trying to write up a script which will grab the selected values from a drop down form, and hide li's if its class does not contain the values from the drop down form.. if that makes sense?? Each li has multiple classes..

Here's what I've got so far (sorry if it's crude):

FORM:

<form action="" name="filter" id="filter" method="post">
        <select name="bedrooms">
          <option value="">- Select Bedrooms -</option>
          <option value="bed-1">1</option>
          <option value="bed-2">2</option>
          <option value="bed-3">3</option>
          <option value="bed-4">4</option>
          <option value="bed-5">5</option>
        </select>
        <select name="bathrooms">
          <option value="">- Select Bathrooms -</option>
          <option value="bath-1">1</option>
          <option value="bath-2">2</option>
          <option value="bath-3">3</option>
          <option value="bath-4">4</option>
          <option value="bath-5">5</option>
        </select>
        <select name="frontage">
          <option value="">- Select Frontage Size -</option>
          <option value="frontage-100">100</option>
          <option value="frontage-200">200</option>
          <option value="frontage-300">300</option>
          <option value="frontage-400">400</option>
          <option value="frontage-500">500</option>
        </select>
        <input type="submit" name="filter-submit" id="filter-submit" value="Go" />
      </form>

JQuery:

$("#filter-submit").click(function() {

          var foo = [];
          $("#filter :selected").each(function(i, value){
            foo[i] = $(value).val();
          });
          if (foo) {
            $.each(foo, function(index, value){
              if (value) {
                //hide other objects based on "value"
                //$("#portfolio li").hasClass();
            };
            });
          };

        return false;
        });

Ok so where I'm stuck is how to hide all "#portfolio li"'s which don't have the class which is outputted as "value". I thought I could use hasClass but not sure how to reverse it.. if that makes sense? Any help would be appreciated :)

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

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

发布评论

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

评论(3

指尖凝香 2024-09-23 14:07:48

您可以使用 .not() 并传递该类,如下所示:

$("#filter-submit").click(function() {
  var lis = $("#portfolio li");          //get all of the <li> elements
  $("#filter select").each(function() {  //loop through selected classes
    var val = $(this).val();             //get selected value, could be blank
    if(val) lis = lis.not('.' + val);    //get only <li>s without the class
  });
  lis.hide();                            //hide the ones without all classes
  return false;                          //prevent default behavior
});

You could use .not() and pass the class, like this:

$("#filter-submit").click(function() {
  var lis = $("#portfolio li");          //get all of the <li> elements
  $("#filter select").each(function() {  //loop through selected classes
    var val = $(this).val();             //get selected value, could be blank
    if(val) lis = lis.not('.' + val);    //get only <li>s without the class
  });
  lis.hide();                            //hide the ones without all classes
  return false;                          //prevent default behavior
});
空‖城人不在 2024-09-23 14:07:48

只是想我会把这个扔出去好玩。采用稍微不同的方法来隐藏

  • 元素。在一个选择器中完成所有操作。
  • $("#filter-submit").click(function() {
        var value = $('#filter select').map(function() {
            return ($(this).val() || null);
        }).get().join( '):not(.' );
    
        if(value) $('#portfolio li:not(.' + value + ')').hide();// Only filter if at
    });​                                                         // least one selected
    

    value 变量将保存一个类似这样的字符串:

    "bed-1):not(.bath-1):not(.frontage-100"
    

    Just thought I'd throw this one out there for fun. Takes a little different approach to hiding the <li> elements. Does it all in one selector.

    $("#filter-submit").click(function() {
        var value = $('#filter select').map(function() {
            return ($(this).val() || null);
        }).get().join( '):not(.' );
    
        if(value) $('#portfolio li:not(.' + value + ')').hide();// Only filter if at
    });​                                                         // least one selected
    

    The value variable will hold a String something like this:

    "bed-1):not(.bath-1):not(.frontage-100"
    
    海拔太高太耀眼 2024-09-23 14:07:48
    $("#filter-submit").click(function()
    {
        var Portfolio = $("#portfolio");
        $('li',Portfolio).show();
    
        $("#filter selectb :selected").each(function(i){
            Class = $(this).attr('value');
            $('li',Portfolio).not('.' + Class).get().hide();
        });
        return false;
    });
    

    试一试吧!

    $("#filter-submit").click(function()
    {
        var Portfolio = $("#portfolio");
        $('li',Portfolio).show();
    
        $("#filter selectb :selected").each(function(i){
            Class = $(this).attr('value');
            $('li',Portfolio).not('.' + Class).get().hide();
        });
        return false;
    });
    

    give that a go!

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