doulao2916 2014-09-28 10:31
浏览 51
已采纳

jQuery:保存已选择的选择元素选项

Hey guys I am trying to save the option that a user has selected on my form however I am unsure how I can do this.

I will briefly explain my setup...

I have this form on my home page:

<form class="form-home form-search">
<select 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>
 <button href="used-cars.php">Search</button>
</form>

As you can see it is using PHP/MySQL to show the options available.

Okay so I then have this form on another page however the CSS styling is slightly different and it includes a few different select elements.

So when a user has selected an element on the home page all the button does is href to the used-cars.php which lists all of the results.

How can I make it so that jQuery saves the option the user selected on the home page and loads the used-cars.php with those options selected?

Any examples would be great.

EDITED Example of my second form:

<div class="container con-col-listing">
    <div class="row">
      <div class="col-md-4 col-sm-4">
       <form class="car-finder-container dflt-container">
         <h2 class="h2-finder">Car finder</h2>
         <ul class="toggle-view">
           <li class="li-toggle">
            <h4 class="h4-finder-toggle">Make<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4>
            <div class="panel">
             <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 value="'.$make["Make"].'">'.$make["Make"].'</option>
                 ';
                 } ?>
             </select>
             <select class="form-control last-select select-box">
                 <option value="model-any">Model (Any)</option>
                 <?php while($model = $modelFilter->fetch(PDO::FETCH_ASSOC))
                 {
                 echo '
                 <option value="'.$model["Model"].'">'.$model["Model"].'</option>
                 ';
                 } ?>
             </select>
            </div>
           </li>
           <li class="li-toggle">
            <h4 class="h4-finder-toggle">Body type<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4>
            <div class="panel">
             <input id="four-by-four-checkbox" class="float-checkbox" type="checkbox"/>
             <label for="four-by-four-checkbox" class="label-checkbox">4x4</label>
             <input id="convertible-checkbox" class="float-checkbox" type="checkbox"/>
             <label for="convertible-checkbox" class="label-checkbox">Convertible</label>
             <input id="coupe-checkbox" class="float-checkbox" type="checkbox"/>
             <label for="coupe-checkbox" class="label-checkbox">Coupe</label>
            </div>
          </li>
          <li class="li-toggle">
            <h4 class="h4-finder-toggle">Transmission<span class="glyphicon glyphicon-plus glyph-plus-toggle"></span></h4>
            <div class="panel">
             <input id="automatic-checkbox" class="float-checkbox" type="checkbox"/>
             <label for="automatic-checkbox" class="label-checkbox">Automatic</label>
             <input id="manual-checkbox" class="float-checkbox" type="checkbox"/>
             <label for="manual-checkbox" class="label-checkbox">Manual</label>
             <input id="semi-auto-checkbox" class="float-checkbox" type="checkbox"/>
             <label for="semi-auto-checkbox" class="label-checkbox">Semi automatic</label>
            </div>
          </li>
        </ul>
         <button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars 
         </button>
         <h4 class="h4-finder"><a href="#">Try our Smart Search </a><span class="glyphicon info-car-search-g glyphicon-info-sign"></span></h4>
       </form>
      </div>
  • 写回答

1条回答 默认 最新

  • dougezhua0017 2014-09-28 10:41
    关注

    Easiest way is with a get request:

     <form class="form-home form-search" method="GET" action="used-cars.php">
      <select name="make" class="form-control select-box">
      .....
      <button type="submit">Search</button>
     </form>
    

    and on used-cars.php:

     var_dump($_GET['make']);
    

    EDIT

    lets suppose on the form on the first page you have:

      <option value="ford">ford</option>
      <option value="pontiac">pontiac</option>
      <option value="fiat">fiat</option>
    

    when you hit the submit button $_GET['make'] on used-cars.php will be whatever was selected lets say fiat.

    so now on used-cars.php you can do this:

     <?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC)){
        $selected = $make['Make'] == $_GET['make']?'selected="selected"':'';
        echo '
         <option '.$selected.' value="'.$make["Make"].'">'.$make["Make"].'</option>
            ';
      } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看