douhui8025 2016-02-27 23:21
浏览 37

最后一次下拉不依赖于之前的下拉列表

I have a list of dropdowns that are base off the previous one. They pull their data based on the selection of the previous selection and populate the next dropdown. I am using the same code, but for some reason, it is not working on the last one. Here is the JavaScript:

$(document).ready( function() { 
    $("#order").change(function() {
        $(this).after('<div id="loader"><img src="img/ajax-loader.gif" alt="loading subcategory" /></div>');
        $.get('loadFamilies.php?order=' + $(this).val(), function(data) {
            $("#family").html(data);
            $('#loader').slideUp(200, function() {
                $(this).remove();
            });
        }); 
    }); 

    $("#family").change(function() {
        $(this).after('<div id="loader"><img src="img/ajax-loader.gif" alt="loading subcategory" /></div>');
        $.get('loadSubFamilies.php?family=' + $(this).val(), function(data) {
            $("#subfamily").html(data);
            $('#loader').slideUp(200, function() {
                $(this).remove();
            });
        }); 
    }); 

    $("#subfamily").change(function() {
        $(this).after('<div id="loader"><img src="img/ajax-loader.gif" alt="loading subcategory" /></div>');
        $.get('loadGenus.php?subfamily=' + $(this).val(), function(data) {
            $("#genus").html(data);
            $('#loader').slideUp(200, function() {
                $(this).remove();
            });
        }); 
    }); 

    $("#genus").change(function() {
        $(this).after('<div id="loader"><img src="img/ajax-loader.gif" alt="loading subcategory" /></div>');
        $.get('loadSpecies.php?genus=' + $(this).val(), function(data) {
            $("#species").html(data);
            $('#loader').slideUp(200, function() {
                $(this).remove();
            });
        }); 
    }); 
});

Here is the HTML:

<form method="get">
    <label for="order">Order</label>
    <select name="order" id="order">
        <option value="0">Select an Order</option>
        <?php 
            $orders = get_ID_and_name_of_orders();
            foreach($orders as $key => $value) { ?>
                <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
        <?php } ?>
    </select>
    <br/><br/>

    <label>Family</label>
    <select name="family" id="family"></select><br/><br/>
    <label>Sub Family</label>
    <select name="subfamily" id="subfamily"></select><br/><br/>
    <label>Genus</label>
    <select name="genus" id="genus"></select><br/><br/>
    <label>Species</label>
    <select name="species" id="species"></select><br/><br/>
</form>

Here is loadGenus.php for getting the genus:

$subfamily = $_GET['subfamily'];
$genus = get_genus($subfamily); // from database
echo("<option value='0'>Select a Genus (optional)</option>");
foreach($genus as $key => $value) {
    echo("<option value='$key'>$value</option>");
}

Here is loadSpecies.php for getting the speicies:

$genus = $_GET['genus'];
$species = get_species($genus);
echo("<option value='0'>Select a Species (optional)</option>");
foreach($species as $key => $value) {
    echo("<option value='$key'>$value</option>");
}

All of the dynamic dropdowns work except for the species one. I have looked at this for hours and debugged everything I can and cannot see why the options are not being populated in the species dropdown. I have isolated the call to loadSpecies.php and it returns with the correct list of species options by itself. The JavaScript runs with no errors. No errors in the console.

I am hoping another set of eyes might see something I am missing here. Thank you in advance!

code for get_Species: function get_species($genus_id) { global $database; $sql = $database->query("SELECT id, species_name FROM species where genus_id = ".$database->escape_value($genus_id)." ORDER BY species_name"); $speciesArray = array(); while ($row = $sql->fetch_assoc()) { $speciesArray[$row['id']] = $row['species_name']; } return $speciesArray; }

  • 写回答

1条回答 默认 最新

  • dpppic5186 2016-03-01 02:24
    关注

    User error! I had an require statement in my php that was including other HTML that had a an element with an id of species--an ambiguous id reference. I changed the id to another name and it worked.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助