无法检索使用jQuery Ajax调用发送的数据.load()

时间:2011-12-06 07:24:18

标签: php jquery ajax select load

我尝试在服务器端检索使用selected option调用发送的jQuery .load()的值,如下所示:

<script type="text/javascript">
        $(function () {
          $("#first").change(function () {
            $("#second").load('populate_section.php?year=', {first: $(this).val()});
          });
        });
</script>

<tr>
  <td>
    <select id="first" name="year">
      <option value=""></option>
      <option value="1">1</option>
      <option value="2">2</option>
      </select>
  </td>
</tr>
<tr>
  <td>
    <select id="second" name="section">
    </select>
  </td>
</tr>

然后我如何尝试检索PHP代码中的值:

<?php
$year = $_GET['year']; // for test only
echo "<option>" . $year . "</option>";
?>

我在这里假设结果将是一个下拉框,其值为我在第一个下拉列表中选择的值。但是,似乎var $year为空。

感谢。

2 个答案:

答案 0 :(得分:1)

您尚未在网址中为“年” GET变量设置任何值。 如果您需要将 $(this).val()作为“年”发送,请执行以下代码。

$("#second").load('populate_section.php', {"year": $(this).val()});

答案 1 :(得分:0)

试试这个:

    $(function () {
      $("#first").change(function () {
        $("#second").load('populate_section.php', {year: $(this).val()});
      });
    });