[英]Ajax Country City Select Menu onChange() not working
我遇到了一个很好的教程,用于创建可以动态更改的下拉菜单。
链接在这里。
这是演示
http://roshanbh.com.np/dropdown/
我正在摆弄代码,我正在跳过你选择状态的部分。
这是findCity.php文件
<!--//---------------------------------+
// Developed by Roshan Bhattarai |
// http://roshanbh.com.np |
// Contact for custom scripts |
// or implementation help. |
// email-nepaliboy007@yahoo.com |
//---------------------------------+-->
<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>
<? $country = $_GET['country'];
$link = mysql_connect("snip","snip","snip");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('snip');
$query="SELECT city FROM location WHERE country='$country'";
$result=mysql_query($query);
?>
<select name="city">
<option>Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
用country变量打开findCity.php工作正常
http://globatum.com/admin/findCity.php?country=United%20States
我无法弄清楚为什么国家/地区列表中的值没有正确地通过此功能
function getCity(country) {
var strURL="findCity.php?country="+ country;
var req = getXMLHTTP();
if (req) {
req.onreadycountrychange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
这里有什么想法?
添加说明
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
你在页面上有一个javascript错误,@nevermind打败了我指出。 那很好,但是你有另一个问题似乎只是对你的误解。 这条线
req.onreadycountrychange = function() {
没有意义,需要改为:
req.onreadystatechange = function() {
函数onreadystatechange
的名称在这种情况下有点令人困惑,但它与'State'和'State / Country'没有任何关系。 这是指XMLHttpRequest
对象的当前状态。 基本上,只要更新请求对象的状态,就会调用onreadystatechange
。
在这里阅读更多相关信息: http : //www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
您在测试页上的代码中有语法错误:
if (req.readyState == 4 {
当然应该是: if (req.readyState == 4 )
如果这不能解决问题,我们可以尝试其他方法。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.