我需要使用JavaScript隐藏基于其他表单值的表单。
假设我们有以下HTML:
<div class="form-row">
<div class="form-group col-2 0 mb-0" id="one" >
{{form.x|as_crispy_field}}
</div>
<div class="form-group col-2 0 mb-0 d-none" id="two">
{{form.y|as_crispy_field}}
</div>
</div>
现在,假设我要隐藏y是x的形式,其值等于“隐藏”。如何使用jQuery获得它?
我已经尝试了以下代码,但是它不起作用:
function check_field_value() {
if($(this).val() == 'Hide') {
$('#two').removeClass('d-none');
} else {
$('#two').addClass('d-none');
}
}
// this is executed once when the page loads
$(document).ready(function() {
$('#one').change(check_field_value);
check_field_value.call($('#one').get(0));
});