【发布时间】:2018-04-14 23:14:07
【问题描述】:
我正在使用 Jquery-nice-select 插件。(http://hernansartorio.com/jquery-nice-select/)。
我有两个选择下拉菜单。在第一个中,我显示国家名称表单数据库,在第二个下拉列表中,我根据国家名称显示州名称。
我可以同时显示两者,但问题是,当我选择国家名称时,会显示州名称,但在选择下拉菜单中没有显示。即使我无法选择它。请检查下图。 你能帮我解决这个问题吗?
之前
选择国名后
<?php
include('connection.php');
$country_list="SELECT id,name FROM countries";
/* create a prepared statement */
if ($stmt = $conn->prepare($country_list)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($id, $country_name);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/nice-select.css">
</head>
<body>
<!--country name-->
<select name="country_data" class="myselect form-control country_data">
<option value="" disabled selected>Select your country</option>
<?php
while ($stmt->fetch()) {?>
<option value="<?php echo $id;?>"><?php echo $country_name;?></option>
<?php }?>
</select>
<!--state name-->
<select name="state" class="myselect form-control state_get">
<option value=''>Select state</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="js/jquery.nice-select.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('select').niceSelect();
});
$(document).ready(function() {
$(".country_data").on('change',function(){
var country_id=$(this).val();
$.ajax({
url:"process.php?key=state_retrive",
method:"POST",
data:'id='+country_id,
success:function(data){
$('.state_get').html(data);
//alert(data);
}
});
});
});
</script>
</body>
</html>
Process.php
<?php
ob_start();
session_start();
include('connection.php');
switch($_GET['key']) {
case 'state_retrive':state_retrive($conn);break;
default : redirect('index.php');
}
function state_retrive($conn)
{
if (isset($_POST['id'])) {
$country_id=$conn->real_escape_string(trim($_POST['id']));
$state_data="SELECT name,id FROM `states` WHERE country_id=?";
//echo $state_data;
if ($stmt = $conn->prepare($state_data)) {
/* bind parameters for markers */
$stmt->bind_param("s", $country_id);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($state_name, $id);
/* fetch value */
echo $states="<option value=''>Select state</option>";
while ($stmt->fetch()) {
$states="<option value=".$id.">".$state_name."</option>";
echo $states;
}
/* close statement */
$stmt->close();
}
$conn->close();
}
}
?>
【问题讨论】:
-
能否请您添加一个我们可以访问的链接或演示?
-
@OfirBaruch,我应该在哪里添加它?因为数据来自数据库。我只有上面的代码。
-
Amm... 使用浏览器的检查工具(开发者控制台 -> 元素选项卡)并在为您的问题选择国家后添加下拉列表的输出。
-
-
@OfirBaruch,我在网络选项卡->响应中得到了这个输出
标签: javascript php jquery html select