简体   繁体   English

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

[英]jQuery: Save a select element option that has been selected

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. 如您所见,它正在使用PHP / MySQL来显示可用选项。

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. 好的,那么我在另一页上有了此表单,但是CSS样式略有不同,它包含一些不同的select元素。

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. 因此,当用户在主页上选择了一个元素时,所有按钮的作用就是href指向used-cars.php,该列表列出了所有结果。

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? 我怎样才能使jQuery保存用户在主页上选择的选项,并使用选择的选项加载used-cars.php?

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>

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: 并在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 . 当您点击used-cars.php上的Submit按钮$_GET['make'] ,将被选中为fiat

so now on used-cars.php you can do this: 因此,现在在used-cars.php上,您可以执行以下操作:

 <?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC)){
    $selected = $make['Make'] == $_GET['make']?'selected="selected"':'';
    echo '
     <option '.$selected.' value="'.$make["Make"].'">'.$make["Make"].'</option>
        ';
  } ?>

暂无
暂无

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

相关问题 jQuery - 用于检测是否已选择HTML select选项的事件处理程序 - jQuery - event handler to detect if HTML select option has been selected 是否有一条指令允许选择一个选项后隐藏选择元素? - Is there a directive which allows a select element to be hidden once an option has been selected? 如何使已添加到选择标签的新选项成为jQuery中选择的 - how to make the new option that has been added to a select tag as selected in Jquery 选择一个选项后,打开HTML选择标签 - Open an HTML select tag after one option has been selected 选择从选择下拉菜单中的选项时出现的模态 - modal appearing when option from select dropdown has been selected 当“不相关”的html select元素未选择任何选项时,如何退出jQuery事件? - How can I exit from a jQuery event when an “unrelated” html select element has had no option selected? 当从另一个 select 中选择了特定选项时,隐藏多个 select 中的选项 - Hide an option in a multiple select, when a specific option has been selected from another select 确保多个选择元素已选择非空选项 - Ensure Multiple Select Element Has Non Empty Option Selected 如果已通过“选择”框的更改事件“选择”或“取消选择”一个选项,则查找 - FInd if an option has been 'selected' OR 'de-selected' via change event of Select box 如何使用jquery检查所有选择框是否都选择了选项? - How to check if all select boxes has selected option using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM