【问题标题】:jQuery show and hide dynamic classes not workingjQuery显示和隐藏动态类不起作用
【发布时间】:2014-09-22 21:27:43
【问题描述】:

我正在尝试使用选择元素隐藏和显示页面上显示的 div,但是遇到了一些麻烦,因为我似乎无法让 jQuery 运行。

我正在使用 PHP 列出我的 SQL 表中的结果,该表当前在我的页面上显示每一行并将它们打印到列表中。

我想让 jQuery 隐藏没有与所选选择选项匹配的类的 div。

这里是一个列表模板的例子,它回显出所有的 MySQL 结果并将它们显示到一个模板中,然后循环显示表格上的每一行:

<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
      {
      echo '
        <div class="listing-container ' . $row["Make"] . '">
          <a href="carpage.php"><h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3></a>
          <h3 class="price-listing">£'.number_format($row['Price']).'</h3>
        </div>
        <div class="listing-container-spec">
         <img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/>
          <div class="ul-listing-container">
            <ul class="overwrite-btstrp-ul">
              <li class="diesel-svg list-svg">'.$row["FuelType"].'</li>
              <li class="saloon-svg list-svg">'.$row["Bodytype"].'</li>
              <li class="gear-svg list-svg">'.$row["Transmission"].'</li>
              <li class="color-svg list-svg">'.$row["Colour"].'</li>
            </ul>
          </div>
          <ul class="overwrite-btstrp-ul other-specs-ul h4-style">
            <li>Mileage: '.number_format($row["Mileage"]).'</li>
            <li>Engine size: '.$row["EngineSize"].'cc</li>
          </ul>
          <button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked 
          </button>
          <button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details 
          </button>
          <button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive 
          </button>
          <h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4>
        </div>
          ';
      } ?>

“Make”被添加到列表容器 div 中以添加一个能够使用 jQuery 过滤结果的类。

这是我正在使用的带有选择元素的表单:

<form>
<select class="form-control select-box">
                 <option value="make-any">Make (Any)</option>
                 <?php while($make = $filterres->fetch(PDO::FETCH_ASSOC))
                 {
                 echo '
                 <option>'.$make["Make"].'</option>
                 ';
                 } ?>
             </select>
             <select class="form-control last-select select-box">
                 <option value="model-any">Model (Any)</option>
                 <option value="two">Two</option>
                 <option value="three">Three</option>
                 <option value="four">Four</option>
                 <option value="five">Five</option>
             </select>
</form>

如您所见,选择选项包含“Make”并且是循环的。

至于 jQuery:

<script>//Wait for DOM to load
(function() {
    $(“.select-box”).change( function() {

        // get the value of the select element
        var make = $(this).val();


        //get all of the listing-container divs, remove the ones with the selected make class, then hide the rest
        $(“.listing-container”).not(“.” + make).hide();
    });
});</script>

所以理论上这应该有效,但由于某种原因它不是,任何人都可以注意到任何可能有问题的地方吗?

我已将脚本放在页脚中的核心 jQuery 下方,但它仍然无法正常工作。

这是一个活生生的例子:http://www.drivencarsales.co.uk/used-cars.php

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    看起来您在该页面的源代码中使用了错误的引号尝试将它们替换为"

    //Wait for DOM to load
    $(function() {
        $(".select-box").change( function() {
    
            // get the value of the select element
            var make = $(this).val();
    
    
            //get all of the listing-container divs, remove the ones with the selected make class, then hide the rest
            $(".listing-container").not("." + make).hide().next().hide();
        });
    });
    

    编辑 你还需要一个$ 在函数之前

    【讨论】:

    • 为什么要投反对票?如果您愿意检查,OP 会收到 Uncaught SyntaxError: Unexpected token ILLEGAL
    • 同意,至少部分代码已在 Word 或写字板中编辑,生成“smartQuotes”。修复它们是必须的,但不是对所提出问题的回答。
    【解决方案2】:

    如果我理解正确的话 ↓ 工作代码

    $(function() {
       $('.select-box').on("change",function() {
           var make = this.value;
           $('div.listing-container.'+make+",div.listing-container."+make+" + div.listing-container-spec").show();
           $('div.listing-container:not(.'+make+'),div.listing-container:not(.'+make+') + div.listing-container-spec').hide();
       });
    });
    

    更短的代码(但更慢):

    $(function() {
       $('.select-box').on("change",function() {
           var make = this.value;
           $('.listing-container.'+make+",.listing-container."+make+" + div").show();
           $('.listing-container:not(.'+make+'),.listing-container:not(.'+make+') + div').hide();
       });
    });
    

    P.S.你错过了 value 属性(但在现场示例中一切正常):

    echo '<option value="'.$make["Make"].'">'.$make["Make"].'</option>';
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 2012-12-29
      • 2012-11-01
      • 2017-09-22
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      相关资源
      最近更新 更多