简体   繁体   English

使用GET表单时,jQuery隐藏功能不起作用

[英]jQuery hide function doesn't work when using GET form

Hey guys sorry for the title being a bit misleading it's a bit of tricky question. 大家好,抱歉标题有点误导,这是一个棘手的问题。

So anyway I will first go over my current setup of my two web pages. 因此,无论如何,我将首先查看我当前的两个网页设置。

Basically I have this form on my homepage: 基本上我的主页上有以下表格:

<form class="car-finder-container" method="GET" action="used-cars.php">
 <select name="make" class="form-control select-box">
  <option value="make-any">Make (Any)</option>
  <?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC))
  {
  echo '
  <option value="'.$make["Make"].'">'.$make["Make"].'</option>
  ';
  } ?>
 </select>
</form>

The select element just loops a column of my SQL table to create the options, it's then using GET to select the same option that has been selected by the user on the used-cars.php page. select元素仅循环我的SQL表的一列以创建选项,然后使用GET选择用户在used-cars.php页面上选择的相同选项。

Here is the second page form code: 这是第二页的表单代码:

<form class="car-finder-container dflt-container">
 <select name="make" class="form-control select-box">
  <option value="make-any">Make (Any)</option>
                 <?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC)){
    $selected = $make['Make'] == $_GET['make']?'selected="selected"':'';
    echo '
  <option '.$selected.' value="'.$make["Make"].'">'.$make["Make"].'</option>
        ';
  } ?>
 </select>
</form>

This part of the code is working fine it selects the option selected based on GET, I do have one issue though. 这部分代码工作正常,它选择了基于GET选择的选项,不过我确实有一个问题。

The form is used to filter what div's are shown on my page, all of the div's are in a list and the form simply uses jQuery to hide classes that don't have the same value. 该表单用于过滤在我的页面上显示的div,所有div都在列表中,并且表单仅使用jQuery来隐藏不具有相同值的类。

This is the jQuery I am using: 这是我正在使用的jQuery:

<script>
    $(document).ready(function(){
  $('.form-control').change(function(){
    var make = $(this).val();
    if(make != 'make-any'){
      $('.makes').hide();
      $('.'+make).show();
      } else {
        $('.makes').show();
        }
    });
  });</script>

The jQuery hides classes when the user selects an option, any div's that don't have the same class are hidden. 当用户选择一个选项时,jQuery会隐藏类,所有不具有相同类的div都会被隐藏。

The issue I am having is that when the page loads div's are showing even if they don't match the value that the user selected on page one, the select element has the option the user selected on page one but all the values that don't match it aren't hidden. 我遇到的问题是,即使页面加载的div与用户在第一页上选择的值不匹配,当页面显示div时,select元素仍具有用户在第一页上选择的选项,但所有不匹配的值不会被隐藏。

Any idea why this might be happening? 知道为什么会这样吗?

Here is a live example so you can take a look: http://www.drivencarsales.co.uk/index.php 这是一个实时示例,因此您可以看一下: http : //www.drivencarsales.co.uk/index.php

Simply pick a 'Make' and press 'Search cars' and you will notice that listed div's aren't hidden yet if you press the 'Make' tab you will notice that the 'Make' from page one is selected. 只需选择一个“制造”并按“搜索汽车”,您会发现尚未隐藏列出的div,如果您按“制造”标签,您会注意到已选择了第一页的“制造”。

This is because you are hiding the mismatched <div> s only when the value of <select> changes. 这是因为仅当<select>的值更改时,才隐藏不匹配的<div> Which does not occur by default when the page loads. 页面加载时默认情况下不会发生这种情况。

You can manually trigger a change event on the <select> using the trigger() method after page load to run your script as follows: 您可以在页面加载后使用trigger()方法在<select>上手动触发change事件,以如下所示运行脚本:

$(document).ready(function(){
  $('.form-control').change(function(){
    var make = $(this).val();
    if(make != 'make-any'){
      $('.makes').hide();
      $('.'+make).show();
     } else {
       $('.makes').show();
    }
  });
  $('.form-control').trigger("change");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM