gpt4 book ai didi

php - Ajax 乡村城市选择菜单 onChange() 不起作用

转载 作者:行者123 更新时间:2023-12-02 18:34:12 26 4
gpt4 key购买 nike

我发现了一个很棒的教程,用于创建可以动态更改的下拉菜单。

这是链接。

http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html

这是演示

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>

使用国家/地区变量打开 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在这种情况下有点令人困惑,但它与“州/国家”中的“州”没有任何关系。这是指 XMLHttpRequest 对象的当前状态。基本上,每当请求对象的状态更新时,就会调用该函数 onreadystatechange

在这里阅读更多相关信息:http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

关于php - Ajax 乡村城市选择菜单 onChange() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com