ajax selectbox提供“未定义”值

时间:2019-06-20 09:12:10

标签: php jquery html ajax select

我已经使用ajax根据category的选择来填充选择框type的结果,但结果显示每种类型的选择都不确定。 该代码一直工作到console.log(category);,但结果显示为未定义

HTML

<label class="control-label col-md-2">Type</label>
<div class="col-md-3 ">
    <select  class="form-control" name="type" id="type" required>
        <option selected disabled>Choose Type</option>
        <?php 
            $sqltype="SELECT * FROM type WHERE cat='Dirt'";
            $resulttype=$conn->query($sqltype);
            while ($rowtype = $resulttype->fetch_assoc())
            {
                echo '<option value="'.$rowtype['code'].'">'.$rowtype['name'].'</option>';
            }
        ?>
        <option  value='Ind'>Indirect</option>
    </select>
</div>
<br><br>
</div>
</div>
<div class="row">
    <div class="form-group">
        <label class="control-label col-md-3">Category</label>
        <div class="col-md-3" >
            <select  class="form-control" name="category" id="category" required>
            </select>
        </div>

javascript

<script type="text/javascript">
    $(document).ready(function(){
        $("#type").change(function(){
            var aid = $("#type").val();
            console.log(aid);
            $.ajax({
                type: "GET",
                url: "data.php",
                data: {id: aid},
                success : function(category1){
                    console.log(category1);
                    $category1 = JSON.parse(category1);
                    console.log(category);
                    $('#category').empty();
                    $category1.each(function(cat){
                        $('#category').append('<option value="'+cat.intcode+'">'+cat.name+'</option>');

                    })
                }
            })
        })
    })
</script>

data.php

<?php 
require 'db.php';
if(isset($_GET['id'])) {
    $aaaid=$_GET['id'];
    $q1="SELECT * FROM data WHERE type = '$aaaid'";
    $result=$conn->query($q1);
    $category = $result->fetch_assoc();
    echo json_encode(category);
}
?>

0 个答案:

没有答案