简体   繁体   English

选择框更改与ajax

[英]select box change with ajax

I tried to build select box that change, with ajax, the values of the second select box. 我试图建立使用ajax更改第二个选择框的值的选择框。

First I choose AREA and than the CITIES in this area. 首先,我选择区域,然后选择该区域中的城市。

Can you please tell me what do I do wrong? 你能告诉我我做错了什么吗?

Client side: 客户端:

<script>
$(function () {

  $("#first").change(function () {
    $("#second").load('recs.php?area_id=' + $(this).val());
  });

});
</script>


<form method="post" action="tosomewhere.php">

    <select id="first" name="area_id">
      <option value="1">1</option>
      <option value="2">2</option>
    </select>

    <select id="second" name="section">  </select>

</form>

Server side: 服务器端:

<?PHP

    include "db.php"

    $areaID = $_GET['area_id'];
    $second_option = "";

    $query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC");
    while($index = mysql_fetch_array($query2)) 
    {
        $id = $index['id'];
        $name  = $index['name'];

        $second_option .= "<option value='$id'>$name</option>";
    }

    echo $second_option;

?>

Try this, 尝试这个,

$(function () {

  $("#first").change(function () {
    $.get('recs.php', {area_id: $(this).val()}, function(data) {
      $("#second").html(data);
    });
  });

});

NOTE: It's better to put an exit after echo . 注意:最好在echo之后放置一个exit

echo $second_option;
exit;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM