更改函数不触发,javascript动态形式


Change function not firing, javascript dynamic form

我不知道为什么没有启动。当我测试代码时,表单会出现,但当我从第一个表单中选择时,会出现注释。我确信,无论出于何种原因,change(函数)都不起作用。

<?php
require("scripts/dbconnect.php");
$stmt = $db->prepare('SELECT name FROM sets');
$stmt->execute();
$data = $stmt->fetchAll();
?>
<select id="first-choice">
<?php foreach ($data as $row): ?>
    <option><?=$row["name"]?></option>
<?php endforeach ?>
</select>
<br />
<select id="second-choice">
    <option>Please choose from above</option>
</select>
<script language=JavaScript>
$("#first-choice").change(function() {
    $("#second-choice").load("getter.php?choice=" + $("select#first-choice option").filter(":selected").val());
});
</script>

您需要首先确保JQuery包含在页面中(它不在您的文章中)。第二,确保在DOM准备就绪时调用您的代码。

$(document).ready(function(){
  $("#first-choice").change(function() {
      $("#second-choice").load("getter.php?choice=" + $("select#first-choice option").filter(":selected").val());
  });
});