如何隐藏基于复选框的值 include 选中时必填(包含下拉框版本)
How to Hide Value Based On CheckBox include Required when Checked (Drop-down box version included)
我希望在复选框被选中时显示文本区域,而在未选中时隐藏它们。
界面可以是 运行 但复选框不可点击。
$(document).ready(function() {
$('#ifbroken').change(function() {
if (this.checked)
$('#dvchk').fadeIn('slow');
else
$('#dvchk').fadeOut('slow');
})
});
#dvchk {
display: none
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
</script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<div class="input-field col s12">
<input type="checkbox" id="ifbroken">
<label for="ifborken">If Broken</label>
</div>
<div class="input-field col s12" id="dvchk">
<label for="Problem">Problem</label></br>
</br>
<textarea name="Problem" style="width:600px; height:200px;"></textarea>
</div>
<div class="input-field col s12" id="dvchk">
<label for="ActionTaken">Action Taken</label></br>
</br>
<textarea name="ActionTaken" style="width:600px; height:200px;"></textarea>
</div>
<div class="input-field col s12" id="dvchk">
<label for="BuyOff">Buy Off</label></br>
</br>
<textarea name="BuyOff" style="width:600px; height:200px;"></textarea>
</div>
您有多个相同的 ID 和无效 HTML 并且加载了太多 jQuery 个文件
这个有效
我将 ID 更改为 class,修复了标签中的拼写错误和无效的 </br>
我也将内联样式移到了样式表中
function toggleField() {
$fld = $(".dvchk").find(":input").prop("required", this.checked);
if (this.checked) $('.dvchk').fadeIn('slow'); // there is alas no fadeToggle(boolean)
else $('.dvchk').fadeOut('slow');
}
$(function() {
$('#ifbroken').on("click", toggleField)
});
.dvchk {
display: none
}
textarea {
width: 600px;
height: 200px;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<form>
<div class="input-field col s12">
<input type="checkbox" id="ifbroken">
<label for="ifbroken">If Broken</label>
</div>
<div class="input-field col s12 dvchk">
<label for="Problem">Problem</label><br /><br />
<textarea name="Problem"></textarea>
</div>
<div class="input-field col s12 dvchk">
<label for="ActionTaken">Action Taken</label><br /><br />
<textarea name="ActionTaken"></textarea>
</div>
<div class="input-field col s12 dvchk">
<label for="BuyOff">Buy Off</label><br /><br />
<textarea name="BuyOff"></textarea>
</div>
<input class="dvchk" type="submit" />
</form>
根据@mplungjan 的回答,我也开发了一个投递箱版本!
JavaScript函数:
function toggleField() {
$fld = $(".dvchk").find(":input").prop("required", this.value =='Yes');
if (this.value == 'Yes') $('.dvchk').fadeIn('slow'); // there is alas no
fadeToggle(boolean)
else $('.dvchk').fadeOut('slow');
}
$(function() {
$('#ifbroken').change(toggleField)
});
HTML 投递箱:
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div class="input-field col s12">
<label for="IfBroken">If Broken</label></br></br>
<select name="IfBroken" id="ifbroken" required>
<option value="">Choose</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
我希望在复选框被选中时显示文本区域,而在未选中时隐藏它们。 界面可以是 运行 但复选框不可点击。
$(document).ready(function() {
$('#ifbroken').change(function() {
if (this.checked)
$('#dvchk').fadeIn('slow');
else
$('#dvchk').fadeOut('slow');
})
});
#dvchk {
display: none
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
</script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<div class="input-field col s12">
<input type="checkbox" id="ifbroken">
<label for="ifborken">If Broken</label>
</div>
<div class="input-field col s12" id="dvchk">
<label for="Problem">Problem</label></br>
</br>
<textarea name="Problem" style="width:600px; height:200px;"></textarea>
</div>
<div class="input-field col s12" id="dvchk">
<label for="ActionTaken">Action Taken</label></br>
</br>
<textarea name="ActionTaken" style="width:600px; height:200px;"></textarea>
</div>
<div class="input-field col s12" id="dvchk">
<label for="BuyOff">Buy Off</label></br>
</br>
<textarea name="BuyOff" style="width:600px; height:200px;"></textarea>
</div>
您有多个相同的 ID 和无效 HTML 并且加载了太多 jQuery 个文件
这个有效
我将 ID 更改为 class,修复了标签中的拼写错误和无效的 </br>
我也将内联样式移到了样式表中
function toggleField() {
$fld = $(".dvchk").find(":input").prop("required", this.checked);
if (this.checked) $('.dvchk').fadeIn('slow'); // there is alas no fadeToggle(boolean)
else $('.dvchk').fadeOut('slow');
}
$(function() {
$('#ifbroken').on("click", toggleField)
});
.dvchk {
display: none
}
textarea {
width: 600px;
height: 200px;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<form>
<div class="input-field col s12">
<input type="checkbox" id="ifbroken">
<label for="ifbroken">If Broken</label>
</div>
<div class="input-field col s12 dvchk">
<label for="Problem">Problem</label><br /><br />
<textarea name="Problem"></textarea>
</div>
<div class="input-field col s12 dvchk">
<label for="ActionTaken">Action Taken</label><br /><br />
<textarea name="ActionTaken"></textarea>
</div>
<div class="input-field col s12 dvchk">
<label for="BuyOff">Buy Off</label><br /><br />
<textarea name="BuyOff"></textarea>
</div>
<input class="dvchk" type="submit" />
</form>
根据@mplungjan 的回答,我也开发了一个投递箱版本!
JavaScript函数:
function toggleField() {
$fld = $(".dvchk").find(":input").prop("required", this.value =='Yes');
if (this.value == 'Yes') $('.dvchk').fadeIn('slow'); // there is alas no
fadeToggle(boolean)
else $('.dvchk').fadeOut('slow');
}
$(function() {
$('#ifbroken').change(toggleField)
});
HTML 投递箱:
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div class="input-field col s12">
<label for="IfBroken">If Broken</label></br></br>
<select name="IfBroken" id="ifbroken" required>
<option value="">Choose</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>