在更改jquery只适用于db值一次

时间:2016-11-13 03:53:13

标签: javascript php jquery ajax

我有3个选择框,其中包含ids首选,第二选择和第三选择。

  <select id="first-choice" style="border-radius: 0; width: 100%;"  class="form-control" name="area">
                                    <option value="">-Select-</option>
                                    <option value="">All</option>
                                    <?php
                                    global $connection;
                                    $area_query = "SELECT DISTINCT area FROM archivo WHERE LENGTH(area) > 1";
                                    $result_area = mysqli_query($connection, $area_query );
                                    while($row_a = mysqli_fetch_array($result_area)):
                                    ?>
                                        <option><?php echo $row_a['area'] ?></option>
                                    <?php endwhile;?>
                                </select>

<select id="second-choice" style="border-radius: 0; width: 100%"  class="form-control" name="category">
                                   <option value="">-Select-</option>

                                </select>

<select id="third-choice" style="border-radius: 0; width: 100%"  class="form-control" name="product">
                                    <option value="">-Select-</option>


                                </select>

我有以下jquery代码:

$(document.body).on('change','#first-choice',function(){
    //alert('Change Happened');
    $("#second-choice").load("../config/select_eng.php?choice=" + $("#first-choice").val());

});

 $(document.body).on('change','#second-choice',function(){
    //alert('Change Happened');
    $("#third-choice").load("../config/select_eng_2.php?choice=" + $("#second-choice").val());

});

事情是它只能工作一次,从第一个选择框到第二个选择框,而不是从第二个选择框到第三个选择框。为什么是这样?请帮忙。

第一个的PHP代码:

$choice = $_GET['choice'];
$query = "SELECT DISTINCT category FROM archivo WHERE area='$choice' AND base = 'Engineering'";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result)) {
echo "<option >" . $row{'category'} . "</option>";

第二个的PHP代码:

$choice = $_GET['choice'];
$query = "SELECT DISTINCT platform_type FROM archivo WHERE category='$choice'";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result)) {
echo "<option value='{$row['platform_type']}'>" .  $row{'platform_type'} . "</option>";

}

1 个答案:

答案 0 :(得分:0)

您需要select_eng.php中第二个的精确副本。

     <select id="second-choice" style="border-radius: 0; width: 100%"  
     class="form-control" name="category" id="area">
           <option value="">-Select-</option>
           //Select option populated from the db
      </select>

你可以将第二个选择放在带有id:

的span
   //use this code in the main page.
  <span id="third-choice">
    <select id="second-choice" style="border-radius: 0; width: 100%"  
      class="form-control" name="category">
         <option value="">-Select-</option>
     </select>
 </span>

 //in select_eng.php, use this               

   $choice = $_GET['choice'];
   $query = "SELECT DISTINCT platform_type FROM archivo 
             WHERE category='$choice'";
   $result = mysqli_query($connection, $query);
   echo '<select id="second-choice" style="border-radius: 0; width: 100%"  
      class="form-control" name="category">';
   while ($row = mysqli_fetch_array($result)) {
      echo '<option value="'.$row['platform_type'].'">'.$row['platform_type'] .'</option>';
   }
    echo '</select>';

id选择器必须是唯一的。你有两个id s in your select. You do not need id =“area”`。希望这可能有所帮助。