[英]How to change a second select list based on the first select list option when values are dynamic?
I have php page for vehicle search filter which have 2 types of vehicle that is RV's and campervans and also have two selects 我有用于车辆搜索过滤器的php页面,其中包含RV和campervans的2种类型的车辆,并且还有两种选择
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Total No. of Passengers*</p>
<select class="half" name="passengers" id="passengers">
<option>No. of Passengers</option>
<option <?php if(@$_REQUEST['passengers'] == 1){?>selected<?php }?>>1</option>
<option <?php if(@$_REQUEST['passengers'] == 2){?>selected<?php }?>>2</option>
<option <?php if(@$_REQUEST['passengers'] == 3){?>selected<?php } ?>>3</option>
<option <?php if(@$_REQUEST['passengers'] == 4){?>selected<?php }?>>4</option>
<option <?php if(@$_REQUEST['passengers'] == 5){?>selected<?php }?>>5</option>
<option <?php if(@$_REQUEST['passengers'] == 6){?>selected<?php }?>>6</option>
<option <?php if(@$_REQUEST['passengers'] == 7){?>selected<?php }?>>7</option>
<option <?php if(@$_REQUEST['passengers'] == 8){?>selected<?php }?>>8</option>
</select>
</div>
How do I do that with jQuery or php if I choose "Rv" in the first select? 如果我在第一次选择中选择“ Rv”,该如何使用jQuery或php做到这一点? The second select would show me 8 passengers .
第二个选择将显示8位乘客。 If I choose Campervan in the first select, the second select would show me 5 passengers..
如果我在第一个选择中选择Campervan,第二个选择将为我显示5位乘客。
I would probably do something along these lines: 我可能会按照以下方式做一些事情:
<?php
$number_of_passengers = 8;
if(!empty($_REQUEST['car_type'])) {
$number_of_passengers = $_REQUEST['car_type'] == 'Campervans' ? 5 : 8;
}
$passengers = 0;
if(!empty($_REQUEST['passengers'])){
$passengers = (int) $_REQUEST['passengers'];
}
?>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<option value="RV/Motorhome">RV/Motorhome</option>
<option value="Campervans">Campervans</option>
</select>
<select class="half" name="passengers" id="passengers">
<option>No. of Passengers</option>
<?php
for($i = 1; $i <= $number_of_passengers; $i++) {
?>
<option
<?php
if($i == $passengers) {
echo ' selected ';
}
?>
>
<?php echo $i ?>
</option>
<?php
}
?>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$('#car_type').change(function() {
$("#passengers").children().removeAttr("selected");
if ($(this).val() === 'Campervans') {
$('#passengers option:nth-child(n+7)').hide();
} else {
$('#passengers option').show();
}
});
</script>
There are cleaner ways of doing this but this should serve to get the point across. 有更清洁的方法可以做到这一点,但这应该有助于理解这一点。
Few things to note about the PHP: 关于PHP的几点注意事项:
See how I check if the $_REQUEST
key is available using empty - this avoids having to use @
which is generally not a good idea. 查看我如何使用空检查
$_REQUEST
密钥是否可用-避免必须使用@
,通常这不是一个好主意。
I am using a loop to echo the options in the passenger select to avoid code repetition because typing that out 8 times is boring ;-) 我正在使用循环来回显乘客选择中的选项,以避免代码重复,因为键入8次很无聊;-)
Hope this helps! 希望这可以帮助!
yes you need to add javascript code for that. 是的,您需要为此添加JavaScript代码。 insert below code in your page and check if this is what you want?
在页面中插入以下代码,然后检查这是否是您想要的?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$('.car').on('change', function(){
if($(this).val() === 'RV/Motorhome'){
$('.half').find('option').show()
}else if($(this).val() === 'Campervans'){
$('.half').find('option').slice(6).hide();
}
})
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.