gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:05:40 26 4
gpt4 key购买 nike

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

<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']}";
}

脚本现在正在工作,我们已经解决了,最后的问题是在处理文件中,当我整理出来时,现在一切都很好

最佳答案

由于您的“系列”数据是动态创建的,因此您需要在 jquery 中使用“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>

关于javascript - 使用 php+mysql 和 jquery ajax post 填充三个后续选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22743233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com