[英]Hide Show multiple form fields using JS loop
I am working on a form which has over 10 text input fields at present and this number is expected to increase over time, each text field is linked with a checkbox, clicking the checkbox enables and disables the input field. 我正在处理一个表单,该表单当前具有10个以上的文本输入字段,并且该数字预计会随着时间的推移而增加,每个文本字段都与一个复选框链接在一起,单击该复选框可启用和禁用输入字段。 This process is working fine but I wanted to improve the JS code.
这个过程运行良好,但是我想改进JS代码。 right now each checkbox has its own JS script and i was wondering if it is possible to somehow use JS loop to achieve the hide/show of fields rather than having individual scripts.
现在,每个复选框都有自己的JS脚本,我想知道是否有可能使用JS循环来实现字段的隐藏/显示,而不是使用单个脚本。
Since the HTML is pretty long so just to demonstrate i am copying two input fields here and their JS 由于HTML很长,因此为了演示,我在这里复制了两个输入字段及其JS
<div class="control-group no_bottom_margin">
<label class="control-label checkbox goal_label_text" for="holiday_travel_timeframe"><input
style="margin-top: 10px" type="checkbox" class="input-small" id="holiday_travel"
name="holiday_travel" value=""><img style=""
src="assets/img/goal_holiday.jpg"><?php _e('Holiday or travel'); ?>
</label>
<div class="controls" id="holiday_travel_timeframe" style="padding-top: 5px; display: none">
<select name="holiday_travel_timeframe" class="input-medium pull-right">
<option value="now">Now</option>
<option value="6_months">6 Months</option>
<option value="1_year">1 Year</option>
<option value="2_years">2 Years</option>
<option value="3_years">3 Years</option>
<option value="4_years">4 Years</option>
<option value="5_years">5 Years</option>
<option value="10_years">10 Years</option>
<option value="15_years">15 Years</option>
</select>
</div>
</div>
<div class="control-group no_bottom_margin">
<label class="control-label checkbox goal_label_text" for="house_deposit_timeframe"><input
style="margin-top: 10px" type="checkbox" class="input-small" id="house_deposit"
name="house_deposit" value=""><img style=""
src="assets/img/goal_house.jpg"><?php _e('House deposit'); ?>
</label>
<div class="controls" id="house_deposit_timeframe" style="padding-top: 5px">
<select name="house_deposit_timeframe" class="input-medium pull-right">
<option value="now">Now</option>
<option value="6_months">6 Months</option>
<option value="1_year">1 Year</option>
<option value="2_years">2 Years</option>
<option value="3_years">3 Years</option>
<option value="4_years">4 Years</option>
<option value="5_years">5 Years</option>
<option value="10_years">10 Years</option>
<option value="15_years">15 Years</option>
</select>
</div>
</div>
The JS that shows/hide the fields 显示/隐藏字段的JS
$(document).ready(function () {
$("#holiday_travel").change(function () {
$("#holiday_travel_timeframe")[$(this).is(":checked") ? 'show' : 'hide']("fast")
}).change();
$("#house_deposit").change(function () {
$("#house_deposit_timeframe")[$(this).is(":checked") ? 'show' : 'hide']("fast")
}).change();
});
I wanted to use the loop to achieve the hide/show of fields, I will really appreciate any help here 我想使用循环来实现字段的隐藏/显示,在此我将不胜感激
How about something like this: 这样的事情怎么样:
$(document).ready(function () {
$(".control-label input:checkbox").change(function () {
$(this).closest(".control-group").find(".controls")[$(this).is(":checked") ? 'show' : 'hide']("fast")
}).change();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.