【发布时间】:2018-04-12 15:13:39
【问题描述】:
到目前为止,我已经使用以下代码为一个元素实现了这一点:
<div class="form-group">
<label class="control-label">Service</label>
<select class="form-control select2 service">
<option>Select Service</option>
<optgroup label="Services">
<option value="service1">Service 1</option>
<option>Service 2</option>
<option>Service 3</option>
<option value="special">Special</option>
</optgroup>
</select>
</div>
<div class="hidden-input">
<div class="col-sm-8">
<div class="form-group">
<label class="control-label">Description</label>
<textarea class="form-control" name="customDesc" rows="2">
</textarea>
</div>
</div>
</div>
<script>
$('.service').on('change', function () {
var $this = $(this),
value = $this.val();
if (value == 'special') {
$('.hidden-input').fadeIn(350);
} else {
$('.hidden-input').fadeOut(250);
}
});
</script>
问题是服务类选择重复,show-hidden div 也是如此,这使得在下拉列表中选择第一个“value=special”值时显示/隐藏所有“hidden-input”元素。
我尝试在针对重复元素实例的显示回调中实现相同的代码,以便在重复项中隐藏或显示隐藏元素,但没有运气:
// For service repeater
$('.repeater').repeater({
show: function () {
$(this).slideDown();
$(this).find('.select2-multiple').select2({
placeholder: "Select Title",
allowClear: true,
});
$(this).find('.select2').select2({
placeholder: "Select Title",
allowClear: true,
});
$(this).find('.service').on('change', function () {
var $this = $(this),
value = $this.val();
if (value == 'special') {
$('.hidden-input').fadeIn(350);
} else {
$('.hidden-input').fadeOut(250);
}
});
},
hide: function (deleteElement) {
if (confirm('Are you sure you want to delete this element?')) {
$(this).slideUp(deleteElement);
}
}
});
有什么建议吗?
【问题讨论】:
-
当你重复服务类时,你不能添加一个像 service-1 这样的枚举器的名称,然后选择隐藏除此之外的所有内容吗? $('div:not(#myid)');
-
带有多个选择/隐藏输入的 HTML 是什么样子的。您只有一个选择和隐藏的示例。很难提供帮助。
标签: javascript jquery forms repeater