jquery与json可靠选择不工作

时间:2012-12-09 19:50:15

标签: jquery json

尝试创建一个从数据库中获取json值的依赖选择函数,但我无法从select传递第二个值来获取最后一个选项,这是我的代码:

<?php
include("../../cons/cons.php");
$busco = "SELECT distinct(HostCountry) as country From schools WHERE 1 order by country asc";
$bus = mysql_query($busco) or die(mysql_error());
?>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1"> 

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {

//first, detect when initial DD changes
$("#states").change(function() {
    //get what they selected
    var selected = $("option:selected",this).val();
    //no matter what, clear the other DD
    //Tip taken from: http://stackoverflow.com/questions/47824/using-core-jquery-how-do-you-remove-all-the-options-of-a-select-box-then-add-on
    $("#cities").children().remove().end().append("<option value=\"\">Select a City</option>");
    //now load in new options if I picked a state
    if(selected == "") return;
    $.getJSON("getcities.php?method=getcities&returnformat=json",{"stateid":selected}, function(res,code) {
        var newoptions = "";
        for(var i=0; i<res.length; i++) {
            //In our result, ID is what we will use for the value, and NAME for the label
            newoptions += "<option value=\"" + res[i].VALOR + "\">" + res[i].NOMBRE + "</option>";
        }
        alert (newoptions);
        $("#cities").children().end().append(newoptions);
    });
});


// mi mano

$("#cities").change(function() {
    //get what they selected
    var selected2= $("select.cities").val();
    alert( selected2);
    //no matter what, clear the other DD
    //Tip taken from: http://stackoverflow.com/questions/47824/using-core-jquery-how-do-you-remove-all-the-options-of-a-select-box-then-add-on
    $("#subject").children().remove().end().append("<option value=\"\">Select a Subject</option>");
    //now load in new options if I picked a state
    if(selected2 == "") return;
    $.getJSON("getsubject.php?method=getsubject&returnformat=json",{"cityid":selected2}, function(res2,code) {
        var newoptions = "";
        for(var i2=0; i2<res2.length; i2++) {
            //In our result, ID is what we will use for the value, and NAME for the label
            newoptions2 += "<option value=\"" + res2[i2].VALOR + "\">" + res2[i2].NOMBRE + "</option>";
        }
        $("#subject").children().end().append(newoptions2);
    });
});
//hasta aca

})
</script>
</head>
<body>


<form method="post" action="">
<select name="states" id="states">
<option value="">Select Country</option>
<?php while ($b = mysql_fetch_array($bus)){
$dato = $b['country'];
$valor = str_replace(" ","_", $dato);
echo "<option value = '$valor'>$dato</option>";
}
?>
</select>


<select name="cities" id="cities">
<option value="">Select a City</option>
</select>  

<select name="subject" id="subject">
<option value="">Select a Subject</option>
</select>   

</form>

</body>
</html>
你能给我一个暗示吗?我在第二次警报时未定义.....谢谢

1 个答案:

答案 0 :(得分:0)

select没有cities,其内容为cities,因此请使用

var selected2= $("#cities").val();

var selected2= $(this).val();

而不是

var selected2= $("select.cities").val();