Javascript 在选择第一个下拉列表时未加载从属下拉列表
我有两个下拉列表。我使用Jquery加载第二个下拉列表。没有jqyery,我的php代码运行良好。但当我使用jquery时,第二个下拉列表在选择第一个下拉列表时变为空 首次辍学教育Javascript 在选择第一个下拉列表时未加载从属下拉列表,javascript,php,jquery,Javascript,Php,Jquery,我有两个下拉列表。我使用Jquery加载第二个下拉列表。没有jqyery,我的php代码运行良好。但当我使用jquery时,第二个下拉列表在选择第一个下拉列表时变为空 首次辍学教育 $sqleducation = "select * from education order by education_id asc "; $reseducation = mysqli_query($conn,$sqleducation); <select name="education" id="educa
$sqleducation = "select * from education order by education_id asc ";
$reseducation = mysqli_query($conn,$sqleducation);
<select name="education" id="education">
<option value="-1">Please Select</option>
<?php while($roweducation=mysqli_fetch_array($reseducation)){ ?>
<option value="<?php echo $roweducation['education_id']?>">
<?php echo $roweducation['education_name']?>
</option>
<?php }?>
</select>
二次降阶
<select name="degree" id="degree" >
<option value="-1">Please Select</option>
<?php if(isset($_POST["education_id"]) && !empty($_POST["education_id"])){
$sqldegree = "SELECT * FROM degree WHERE education_id = ".$_POST['education_id']." ";
$resdegree = mysqli_query($conn,$sqldegree);
while($rowdegree=mysqli_fetch_array($resdegree))
{ ?>
<option value="<?php echo $rowdegree['degree_id']?>">
<?php echo $rowdegree['degree_name']?>
</option>
<?php } }?>
</select>
第二个下拉菜单是使用juery加载第一个下拉菜单的选择
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#education').on('change',function(){
var educationID = $(this).val();
if(educationID){
$.ajax({
type:'POST',
url:'education-career.php',
data:'education_id='+educationID,
success:function(html){
$('#degree').html(html);
}
});
}else{
$('#degree').html('<option value="">Select Education first</option>');
}
});});</script>
在ajax中,您正在使用
data:'education_id='+educationID,
用于发布数据。变量名将显示在这里
在您的第二页中,您试图获得:
isset($_POST["education"])
仅此而已。所以,在第二页中,您必须用教育id替换教育。尝试在下面的行中进行更改
data:'education_id='+educationID,
到
试试这个。第二个选择标记必须放在第一页才能使用$'degree'.html 第一个下拉列表
$sqleducation = "select * from education order by education_id asc ";
$reseducation = mysqli_query($conn,$sqleducation);
<select name="education" id="education">
<option value="-1">Please Select</option>
<?php while($roweducation=mysqli_fetch_array($reseducation)){ ?>
<option value="<?php echo $roweducation['education_id']?>">
<?php echo $roweducation['education_name']?>
</option>
<?php }?>
</select>
<select name="degree" id="degree" >
<option value="-1">Please Select</option>
</select>
第二个下拉列表
<option value="-1">Please Select</option>
<?php if(isset($_POST["education_id"]) && !empty($_POST["education_id"])){
$sqldegree = "SELECT * FROM degree WHERE education_id = ".$_POST['education_id']." ";
$resdegree = mysqli_query($conn,$sqldegree);
while($rowdegree=mysqli_fetch_array($resdegree))
{ ?>
<option value="<?php echo $rowdegree['degree_id']?>">
<?php echo $rowdegree['degree_name']?>
</option>
<?php } }?>
更改:
$('#education').on('change',function(){
致:
我试过了,如果设置$\u POST[education\u id]&&!空的$_POST[education\u id]但没有问题你也必须在这里更改:数据:'education\u id='+educationID,使用这个:数据:{'education\u id';educationID},你做了什么更改?@SaritaSharma,第二个选择标签必须放在第一页才能使用$'degree'。html…@Star。在第二个下拉列表中检查您的where条件。@CodeLღ弗尔,非常感谢。我修好了。@Star\u人嗯,很好,现在很好
$('#education').on('change',function(){
$('select#education').change(function(){