我正在构建一个 Web 表单,我希望能够允许用户从下拉菜单中选择一个选项,并且根据他们选择的值,将生成一个包含与该选项相关的更多字段的 div。我已经成功地为另一个项目实现了这个代码,只有一个选项。我回收了该项目的代码并对其进行了修改以适应多个选项,但到目前为止,我还没有成功运行一个工作产品。
这是我一直在使用的一些源代码来说明我的观点。
jQuery 脚本
$(document).ready(function(){
$("#more_info_bus").hide();
$("#more_info_bus").attr("disabled", "disabled");
$("#more_info_train").hide();
$("#more_info_train").attr("disabled", "disabled");
$("#more_info_tram").hide();
$("#more_info_tram").attr("disabled", "disabled");
$("#transport_type").change(function(){
var val = $(this).val;
$("#more_info_bus input").attr("enabled",(val=="Bus")?"":"enabled");
(val=="Bus")?$("#more_info_bus").show():$("#more_info_bus").hide();
$("#more_info_train input").attr("enabled",(val=="Train")?"":"enabled");
(val=="Train")?$("#more_info_train").show():$("#more_info_train").hide();
$("#more_info_tram input").attr("enabled",(val=="Tram")?"":"enabled");
(val=="Tram")?$("#more_info_tram").show():$("#more_info_tram").hide();
});
});
HTML 代码(缩短为两个 div,而不是所有选项,但你明白了
<label>Is the venue accessible by public transport via:</label> <br />
<select name="transport_type" id="transport_type">
<option disabled selected>Please select a transport type...</option>
<option value="Bus">Bus</option>
<option value="Train">Train</option>
<option value="Tram">Tram</option>
<option value="Taxi">Taxi</option>
<option value="Other">Other</option>
</select> <br /> <br />
<!--HIDDEN DIVS THAT WILL APPEAR UPON SELECTION OF THE VALUES IN THE SELECT ABOVE -->
<div id="more_info_bus">
<label>Bus: Frequency of service</label>
<input type="text" name="bus_freq" value="<?php echo $results['bus_freq']; ?>"/><br />
<label>Bus: route numbers(s)</label>
<input type="text" name="bus_routes" value="<?php echo $results['bus_routes'];?>"/> <br />
<label>Bus: Distance from stop to venue (in meters)</label>
<input type="text" name="bus_dist" value="<?php echo $results['bus_dist'];?>"/> <br />
<label>Bus: How accessible is the route from stop to venue?</label>
<textarea cols="50" rows="2" name="bus_access"><?php echo $results['bus_access'];?></textarea> <br />
</div>
<div id="more_info_train">
<label>Train: Frequency of service</label>
<input type="text" name="train_freq" value="<?php echo $results['bus_freq']; ?>"/><br />
<label>Train: route numbers(s)</label>
<input type="text" name="train_routes" value="<?php echo $results['bus_routes'];?>"/> <br />
<label>Train: Distance from stop to venue (in meters)</label>
<input type="text" name="train_dist" value="<?php echo $results['bus_dist'];?>"/> <br />
<label>Train: How accessible is the route from stop to venue?</label>
<textarea cols="50" rows="2" name="train_access"><?php echo $results['bus_access'];?></textarea> <br />
</div>
有谁知道为什么我的div
部分保持隐藏?当用户选择每个 div 各自的值时,它们不会出现。