【问题标题】:Update the list automatically after selecting the option with JavaScript or jquery使用 JavaScript 或 jquery 选择选项后自动更新列表
【发布时间】:2016-09-30 13:31:09
【问题描述】:

我想在选择另一个列表的选项后更新列表

HTML 代码

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/css/bootstrap-select.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/js/bootstrap-select.min.js"></script>

<form action="check-add.php" method="post">
<table>
<tr>
<td align="center" height="60">students</span></td>
<td align="center" height="60"><select id="first-choice">
<option disabled selected>select students</option>
<option value="1">Mark</option>
<option value="2">Makarious</option>
</select></td>
</tr>
<tr>
<td align="center" height="60">subject</td>
<td align="center" height="60">
 <select class="selectpicker" data-style="btn-info" multiple data-max-options="7" data-live-search="true" id="second-choice"></select>
</td>
</tr>
<tr>
<td align="center" height="100" colspan="2">
<input type="submit" class="but" name="save-students" value="add"/></td>
</tr>
</table>
</form>

JavaScript 代码

<script type="text/javascript">
$("#first-choice").change(function() {
$("#second-choice").load("getter.php?choice=" + $("#first-choice").val());
}).trigger("change");
</script>

getter.php 文件

<?php
    @include("../include/config.php");

    $choice = @mysqli_real_escape_string($conn, $_GET['choice']);
    $result = @mysqli_query($conn,"SELECT * FROM subjects WHERE phases='".$choice."'");
    while($row = @mysqli_fetch_array($result, MYSQL_ASSOC)){
        echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
    }
?>

在此列表中选择变量后 不更新第二个列表 它没有出现选项 在#second-choice

我希望 JavaScript 代码自动更新第二个列表,但在从第一个菜单中选择选项后

【问题讨论】:

    标签: javascript jquery select


    【解决方案1】:

    在 getter.php 中,$choice 将等于第一选择值中的“选择学生”、“1”或“2”之一。这是你所期望的吗?

    我从您的 SELECT 语句中猜测您宁愿收到“”、“Mark”或“Makarious”。如果是这样,那么首选应该是这样的

    <select id="first-choice">
      <option disabled selected value="">select students</option>
      <option value="Mark">Mark</option>
      <option value="Makarious">Makarious</option>
    </select> 
    

    【讨论】:

    • 问题不是第一个选项(first-choice)第二个选项的问题没有更新第二个选项的列表(second-choice)假设选择第一个选项后发送变量到第二种选择使用 getter.php 文件
    • 您是否验证了从 getter.php 返回的值?我怀疑 Javascript 正在运行,但是您从 getter.php 得到一个空的选项字符串
    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多