如何在不刷新页面的情况下刷新下拉列表? [英] How to refresh dropdown without page refresh?
本文介绍了如何在不刷新页面的情况下刷新下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了以下困难:
我正在从 mysql 获取下拉列表的值,我希望该信息显示在该下拉列表中.
I am fetching value of dropdown from the mysql and I want that information to be displayed in that dropdown list.
看到这个:
<select id="location" name="location" class='form-control'>
<option value="0">Select location</option>
<?php
$query = mysql_query("select cityname from city");
while($row = mysql_fetch_assoc($query))
{
echo '<option value="'.$row['cityname'].'">'.$row['cityname']. '</option>';
}
?>
</select>
通过使用此代码,我将值从数据库填充到下拉列表,但为此我需要刷新页面以显示值.
By using this code I am populating the values from database to dropdown list, but for this I need to refresh the page for values to get displayed.
谢谢.
推荐答案
使用 jQuery Ajax
Use jQuery Ajax
yourfile.php
<select id="location" onchange="getState(this.value)" name="location" class='form-control'>
<option value="0">Select location</option>
<?php
$query = mysql_query("select * from city");
while($row = mysql_fetch_assoc($query))
{
echo '<option value="'.$row['cityid'].'">'.$row['cityname']. '</option>';
}
?>
</select>
<select id="state">
</select>
Jquery 脚本
function getState(city_id)
{
var html = $.ajax({
type: "POST",
url: "path/to/ajax/my_ajax.php",
data: "city_id=" +city_id,
async: false
}).responseText;
if(html){
$("#state").html(html);
}
}
AJAX.php
$query = mysql_query("select * from state where city_id=".$_REQUEST['city_id']);
while($row = mysql_fetch_assoc($query))
{
echo '<option value="'.$row['state_id'].'">'.$row['state_name']. '</option>';
}
这篇关于如何在不刷新页面的情况下刷新下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文