我的industry.php代码:
<?php
$parent = array('1'=>'电子及家电','2'=>'照明','3'=>'车辆及配件','4'=>'机械','5'=>'五金工具','6'=>'建材');
$child = array('1'=>array('16'=>'家用电器'));
$id = $_GET['id'];
if ($_GET['name'] == 'p') {
echo json_encode($parent);
} else {
echo json_encode($child[$id]);
}
?>
industry.html代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="saigel.js"></script>
</head>
<body>
<div class="init a_industry">
<select id="parent">
<option value="0">请选择</option>
</select>
<select id="child"><option value="0">请选择</option></select>
</div>
</body>
</html>我的saigel.js代码:
Saigel.init_industry = function() {
var parent = $("#parent");
var child = $("#child");
var stat = 1; $.getJSON("industry.php?name=p", function(data){
html = '<option value="0">请选择</option>';
for(var key in data) {
html += '<option value="' + key + '">' + data[key] + '</option>';
}
parent.html(html);
return false;
});
parent.change(function() {
$.getJSON("industry.php?id=" + this.value, function(data){
html = '<option value="0">请选择</option>';
for(var key in data) {
html += '<option value="' + key + '">' + data[key] + '</option>';
}
child.html(html);
});
return false;
});
}
问题是我选中第一个电子及家电的时候,select框返回的还是请选择,不知道哪里有问题,各位请帮忙!

解决方案 »

  1.   

    LZ可以用firebug等工具查看一下XHR的请求及响应内容
      

  2.   

    看了
    industry.php加载了好几次,
      

  3.   

    有,是一些数据信息,
    {"1":"\u7535\u5b50\u53ca\u5bb6\u7535","2":"\u7167\u660e","3":"\u8f66\u8f86\u53ca\u914d\u4ef6","4":"\u673a\u68b0","5":"\u4e94\u91d1\u5de5\u5177","6":"\u5efa\u6750"}
    加载都是那些
    可是不知道js哪里出错了,上面的例子是可以允许的如果配好了php环境。
      

  4.   

    industry.php?id=1
    这个请求的响应信息呢
      

  5.   

    有:{"16":"\u5bb6\u7528\u7535\u5668"}程序是开始先请求一下industry.php?name=p之后在请求industry.php?id=1 然后又请求industry.php?name=p
    为何最后执行的总是industry.php?name=p
      

  6.   

    {"1":"\u7535\u5b50\u53ca\u5bb6\u7535","2":"\u7167\u660e","3":"\u8f66\u8f86\u53ca\u914d\u4ef6","4":"\u673a\u68b0","5":"\u4e94\u91d1\u5de5\u5177","6":"\u5efa\u6750"}
     这些数据没问题啊。。json数据啊。你输出的是json啊
      

  7.   

    不是,用firebug看的它响应的结果就是那个,可是为何总是一味的响应呢?