Javascript 州立城市下拉列表
我测试过这个 州和城市下拉列表的JavaScript代码。我的问题是,当该程序运行时,如果显示来自相应状态的第一个城市,则无法选择处于相同状态的另一个城市。 如:Javascript 州立城市下拉列表,javascript,Javascript,我测试过这个 州和城市下拉列表的JavaScript代码。我的问题是,当该程序运行时,如果显示来自相应状态的第一个城市,则无法选择处于相同状态的另一个城市。 如: 在马哈拉施特拉邦,展示的城市是孟买。如果我从列表中选择pune,它将不起作用。我想普纳或纳西克市也被选中。有什么想法吗?您需要的是将第二个下拉列表替换为多选下拉列表。 在这里,您可以找到很好的示例: 祝你好运 您需要做的一个小更改请修改html 从 到 添加一个您尝试过的示例pleaseA live演示将是helpful@wille
在马哈拉施特拉邦,展示的城市是孟买。如果我从列表中选择pune,它将不起作用。我想普纳或纳西克市也被选中。有什么想法吗?您需要的是将第二个下拉列表替换为多选下拉列表。 在这里,您可以找到很好的示例:
祝你好运 您需要做的一个小更改请修改html 从 到
添加一个您尝试过的示例pleaseA live演示将是helpful@willem在这里你会得到我想要的@THOMAS抱歉,但是屏幕截图不是很有帮助,我想看一个有点孤立的代码示例,说明您的代码是什么样子的,以及您尝试了什么。在编码方面做了一个小改动。删除onChange=setcities;然后它就开始工作了。请添加一些描述,以便我们能够理解发生了什么,以及这段代码如何帮助解决问题。嗨,Mohammad,我在这里使用了oracle 10g和PHP版本5.7。我在类connect的构造函数中定义了数据库连接,并在类函数中对其进行了扩展,在类函数中使用了connect类的构造函数,以便在类函数的对象调用时自动调用它。oci_parse和oci_execute以及oracle 10g函数用于连接和执行查询。简单地说,我在城市下拉列表中显示更改状态时给出了ajax请求。希望你能理解。谢谢你的反馈。
<select name="City" id="city" onChange="setcities();">
<option value="">Please select a City </option>
</select>
<select name="City" id="city"">
<option value="">Please select a City </option>
</select>
class connect {
public function __construct() {
try {
/ estabilish connection with the db credentials
if (!$conn) {
// display error
}
return $conn;
} catch (Exception $e) {
// throw exception
$e->getMessage();
}
}
}
?>
<?php
include_once("conf.php");
include_once("function.php");
$object_function = new functions;
?>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<div>
<select name="select_data" id="select_state">
<option>Please select</option>
<?php
$country_details = $object_function->state_details();
$i = 0;
while ($i < count($country_details)) {
?>
<option value="<?php echo $country_details[$i]['STATEID'] ?>"><?php echo $country_details[$i]['STATENAME']; ?></option>
<?php
$i++;
}
?>
</select>
</div>
<div>
<select name="select_data" id="select_city">
<option>Please select</option>
</select>
</div>
<?php
error_reporting(E_ALL);
include_once("conf.php");
class functions extends connect {
private $conn = '';
public function __construct() {
$this->conn = parent::__construct();
}
public function state_details() {
try {
$all_states = "select * from LWSTATEMASTER";
$state_connection = oci_parse($this->conn, $all_states);
$execute_query = oci_execute($state_connection);
while ($state_row = oci_fetch_assoc($state_connection)) {
$state_rows[] = $state_row;
}
return $state_rows;
} catch (Exception $e) {
$e->getMessage();
}
}
public function city_details($country_data) {
try {
global $conn;
$rows = array();
$state_value = $country_data['state_value'];
$all_cities = "select CITYID,CITYNAME from LWCITYMASTER where STATEID ='$state_value'";
$city_connection = oci_parse($this->conn, $all_cities);
$execute_query = oci_execute($city_connection);
while ($city_row = oci_fetch_assoc($city_connection)) {
$city_rows[] = $city_row;
}
$this->fetch_city_eachrows($city_rows);
} catch (Exception$e) {
$e->getMessage();
}
}
public function fetch_city_eachrows($city_rows) {
try {
$i = 0;
while ($i < count($city_rows)) {
echo "<option value= " . $rows[$i]['CITYID'] . ">" . $city_rows[$i]['CITYNAME'] . "</option>";
$i++;
}
} catch (Exception $e) {
$e->getMessage();
}
}
}
$obj = new functions;
if (isset($_REQUEST['type']) == 'country') {
$country_data = $_REQUEST;
$obj->city_details($country_data);
}
?>
<script>
$(document).ready(function () {
$("#select_state").change(function () {
var val = $('#select_state').val();
$.ajax({
type: "post",
url: 'function.php',
data: {'state_value': val, 'type': 'country'},
`enter code here` success: function (response) {
$("#select_city").html(response);
$("#select_city").show();
}
});
});
});
</script>