使用php + mysql和jquery ajax post填充三个后续选择列表

时间:2014-03-30 11:07:18

标签: javascript php jquery mysql ajax

好的我已经链接了脚本,现在这是索引文件的代码:

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>

<script type="text/javascript">
    jQuery(document).ready(function(){              

        jQuery("select[name='brand']").change(function(){               

            var brandValue = jQuery("select[name='brand']").val();     

            jQuery.ajax({
                type: "POST",
                url: "handler.php",
                data: ({brand: brandValue, status: 1}),
                beforeSend: function(){ jQuery("#loader").show(); },
                complete: function(){ jQuery("#loader").hide(); },
                success: function(response){
                    jQuery("#results").html(response);
                    jQuery("#results").show();
                }
            });         
        });

        jQuery('body').on('change','select[name="series"]',function(){               

            var seriesValue = jQuery("select[name='series']").val();
            var brandValue = jQuery("select[name='brand']").val();     

            jQuery.ajax({
                type: "POST",
                url: "handler.php",
                data: ({series: seriesValue, brand: brandValue, status: 1}),
                beforeSend: function(){ jQuery("#loader").show(); },
                complete: function(){ jQuery("#loader").hide(); },
                success: function(response){
                    jQuery("#results").html(response);
                    jQuery("#results").show();
                }
            });         
        });

        jQuery('body').on('change','select[name="models"]',function(){               

            var modelsValue = jQuery("select[name='models']").val();
            var seriesValue = jQuery("select[name='series']").val();
            var brandValue = jQuery("select[name='brand']").val();

            jQuery.ajax({
                type: "POST",
                url: "handler.php",
                data: ({models: modelsValue, series: seriesValue, brand: brandValue, status: 1}),
                beforeSend: function(){ jQuery("#loader").show(); },
                complete: function(){ jQuery("#loader").hide(); },
                success: function(response){
                    jQuery("#results").html(response);
                    jQuery("#results").show();
                }
            });         
        });

    });
</script>

Brands: 
<select name="brand">
    <option value="">Please Select</option>
<?php
$brands = get_brands();
foreach($brands as $brand) {
    ?>
    <option value="<?php echo $brand['brand_id']?>"><?php echo $brand['brand_name']; ?></option>
    <?php
}
?>
</select>

<div id="results" style="display:none;"></div>

<div id="loader" style="display:none;"><img src="ajax-loader.gif" alt="loading..."></div>

这是处理文件:

<?php
if(!empty($_POST['brand'])) {
    $curentSeries = get_series($_POST['brand']);
    ?>

    Series: 
    <select name="series">
        <option value="">Please Select</option>
        <?php
        foreach($curentSeries as $ser) {
            ?>
            <option value="<?php echo $ser['series_id']; ?>"><?php echo $ser['series_name']; ?></option>
            <?php 
        }
        ?>
    </select>
    <?php
}
?>

<?php
if(!empty($_POST['series'])) {
    $curentModels = get_models($_POST['brand'], $_POST['series']);
    ?>

    Model: 
    <select name="models">
        <option value="">Please Select</option>
        <?php
        foreach($curentModels as $model) {
            ?>
            <option value="<?php echo $model['model_id']; ?>"><?php echo $model['model_name']; ?></option>
            <?php 
        }
        ?>
    </select>
    <?php
}
?>

<?php
if(!empty($_POST['models'])) {
    echo "<br />brand: {$_POST['brand']}<br />series: {$_POST['series']}<br />model: {$_POST['models']}";
}

脚本现在正在运行,我们想通了,最后的问题是在处理文件中,因为我整理出来,一切都很好

1 个答案:

答案 0 :(得分:1)

由于你的'系列'数据是动态创建的,你需要在jquery.Check中使用'on'事件代码

<script type="text/javascript">
    jQuery(document).ready(function(){              

    jQuery("select[name='brand']").change(function(){               

        var optionValue = jQuery("select[name='brand']").val();     

        jQuery.ajax({
            type: "POST",
            url: "data.php",
            data: ({brand: optionValue, status: 1}),
            beforeSend: function(){ jQuery("#loader").show(); },
            complete: function(){ jQuery("#loader").hide(); },
            success: function(response){
                jQuery("#series").html(response);
                jQuery("#series").show();
            }
        });         
    });

    jQuery('body').on('change','select[name="series"]',function(){               

        var seriesValue = jQuery("select[name='series']").val();     

        jQuery.ajax({
            type: "POST",
            url: "data.php",
            data: ({series: seriesValue, status: 1}),
            beforeSend: function(){ jQuery("#loader").show(); },
            complete: function(){ jQuery("#loader").hide(); },
            success: function(response){
                jQuery("#model").html(response);
                jQuery("#model").show();
            }
        });         
    });

     });
  </script>