2017-09-04 130 views
2

我有两个下拉列表。我正在使用Jquery加载第二个下拉菜单。没有jqyery我的php代码工作正常。但是当我使用jquery时,第二个下拉列表在选择第一个下拉列表时变空。依赖下拉列表不会加载选择第一个下拉列表

第一个下拉列表(教育)

$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> 

回答

0

在这里,在阿贾克斯,您使用的是

data:'education_id='+educationID, 

这是用于发布数据。变量名称将在这里education_id

而在你的第二页你正在试图获得:

isset($_POST["education"]) 

这只。因此,在第二页上,您必须将education替换为education_id。下面一行

+0

我试了一下,如果(isset($ _ POST [ “education_id”])&&空($ _ POST [! “education_id”]))但是没有iffect –

+0

你也必须在这里改变:'data:'education_id ='+ educationID,'使用这个:'data:{'education_id'; educationID},' –

1

尝试改变

data:'education_id='+educationID, 

data:{education_id : educationID}, 
1

试试这个。(第二选择标签在第一页的地方才能使用$('#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 } }?> 
+0

你做了什么改变? –

+0

@SaritaSharma,第二个选择标签必须放在第一页才能使用$('#degree')。html(...)。 –

+0

@Star_Man老兄。在第二个下拉菜单中检查你的位置。 –

0

变化:

$('#education').on('change',function(){ 

要:

$('select#education').change(function(){