0

我有一个名为 tblCityState 的数据库表,其中包含以下数据:

CITY 纽约、洛杉矶、亚特兰大

纽约,加利福尼亚,乔治亚

我有一个绑定到表的下拉列表(ddCity)(从 tblCityState 中选择城市)。

当用户从下拉菜单中选择“洛杉矶”时,我想要一个文本框(tbState)填充“加利福尼亚”。

现在,我可以将 DropDown Item 的值设置为 State,但我希望 Item 值设置为 City。

实现这一目标的最佳/最简单方法是什么../这对我来说非常重要..请帮我一些代码。我到处搜索,但没有得到类似的东西。我是 php 和 ajax 的新手。建议我一些代码.

4

1 回答 1

0

索引.php

<script src="jquery.min.js" type="text/javascript"></script>
<script type='text/javascript' src='jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />

<script type="text/javascript">
$().ready(function() {
$("#customer").autocomplete("state.php", {
        width: 160,
        autoFill: true,
        selectFirst: false
    });

$("#customer").blur(function() {
$("#customer1").val('');
$(".ac_results").next().css('height','0px');
var  vState = $("#customer").val();
        $("#customer1").autocomplete("city.php?state="+vState, {
            width: 160,
            autoFill: false,
            data:{},
            length:0,
        });
    });
});
</script>

<form name="thisForm1" method="post" id="thisForm1" action="" >
<table cellpadding="5" cellspacing="5" width="50%" align="center" class="ac_results">
<tr><td>
Enter state<input name="customer" type="text" id="customer"  value="" style="width:300px;" autocomplete="off"   >
</td><td>


Enter Keyword<input name="customer1" type="text" id="customer1"  value="" style="width:500px;" autocomplete="off" >
</td></tr>
</table>

</form>

状态.php

<?php
$db = mysql_pconnect('localhost','root','');
mysql_select_db('test',$db);
 $q = strtolower($_GET["q"]);

$a = mysql_query("SELECT * FROM state");
while($b = mysql_fetch_object($a))

{
echo "$b->name\n";
}




?>

城市.php

<?php
$db = mysql_pconnect('localhost','root','');
mysql_select_db('test',$db);
 $q = strtolower($_GET["q"]);
$vState = $_REQUEST['state'];

if (!empty($vState)) { 
    $a = mysql_query("SELECT * FROM best_hotel WHERE class = '$vState'");
    while($b = mysql_fetch_object($a)) {
        echo "$b->name\n";
    }

}


?>
于 2013-01-29T11:32:41.807 回答