gpt4 book ai didi

javascript - Jquery 隐藏/取消隐藏复选框更改未按预期运行

转载 作者:行者123 更新时间:2023-11-27 22:57:32 25 4
gpt4 key购买 nike

请原谅我的任何坏习惯,我一直在玩 html 和 jquery 48 小时。

我正在尝试根据选中的复选框隐藏/取消隐藏输入字段。如果选中“无”框,则隐藏所有字段。

我在使用下面显示输入字段的代码时遇到问题,直到复选框被选中/取消选中一次。然后他们开始表现得像他们应该的那样。我还通过取消选中其他框来使“无”框起作用,但这不会像我希望的那样触发字段隐藏。

有什么建议吗?

TLDR:在选中相应的框之前,不应看到 DIV 字段。如果选中“无”框,所有 DIV 字段都应该消失。

$('#checkboxes-0').change(function() {
if (this.checked)
$('#blank_co').fadeIn('slow');
else
$('#blank_co').fadeOut('slow');

});

$('#checkboxes-1').change(function() {
if (this.checked)
$('#fresh_co').fadeIn('slow');
else
$('#fresh_co').fadeOut('slow');

});

$('#checkboxes-2').change(function() {
if (this.checked)
$('#marine_co').fadeIn('slow');
else
$('#marine_co').fadeOut('slow');

});

$('#checkboxes-3').change(function() {
if (this.checked)
$('#rain_co').fadeIn('slow');
else
$('#rain_co').fadeOut('slow');
});

var $others = $('input[name="checkboxes"]').not('#checkboxes-4')
$('#checkboxes-4').change(function() {
if (this.checked) {
$others.prop('checked', false)
}
});
$others.change(function() {
if (this.checked) {
$('#checkboxes-4').prop('checked', false)
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-group">

<label class="col-md-4 control-label" for="checkboxes">
Select any desired backgrounds:</label>
<div class="col-md-4">
<label class="checkbox-inline" for="checkboxes-0">
<input name="checkboxes" id="checkboxes-0" type="checkbox" value="blanks">
Blanks
</label>
<label class="checkbox-inline" for="checkboxes-1">
<input name="checkboxes" id="checkboxes-1" type="checkbox" value="fresh">
Fresh Water
</label>
<label class="checkbox-inline" for="checkboxes-2">
<input name="checkboxes" id="checkboxes-2" type="checkbox" value="marine">
Marine Water
</label>
<label class="checkbox-inline" for="checkboxes-3">
<input name="checkboxes" id="checkboxes-3" type="checkbox" value="rain">
Rain
</label>
<label class="checkbox-inline" for="checkboxes-4">
<input name="checkboxes" id="checkboxes-4" type="checkbox" value="none">
None
</label>
</div>
</div>

<!-- Text input-->
<div id="blank_co" div class="form-group">
<label class="col-md-4 control-label" for="blankbg">Blanks cutoff: (1-99)</label>
<div class="col-md-4">
<input name="blankbg" class="form-control input-md" id="blankbg" required="" type="text" placeholder="" value="10">

</div>
</div>

<!-- Text input-->
<div id="fresh_co" div class="form-group">
<label class="col-md-4 control-label" for="freshbg">Fresh water cutoff: (1-99)</label>
<div class="col-md-4">
<input name="freshbg" class="form-control input-md" id="freshbg" required="" type="text" placeholder="" value="10">

</div>
</div>

<!-- Text input-->
<div id="marine_co" div class="form-group">
<label class="col-md-4 control-label" for="marinebg">Marine water cutoff: (1-99)</label>
<div class="col-md-4">
<input name="marinebg" class="form-control input-md" id="marinebg" required="" type="text" placeholder="" value="10">

</div>
</div>

<!-- Text input-->
<div id="rain_co" div class="form-group">
<label class="col-md-4 control-label" for="rainbg">Rain water cutoff: (1-99)</label>
<div class="col-md-4">
<input name="rainbg" class="form-control input-md" id="rainbg" required="" type="text" placeholder="" value="10">

</div>
</div>

最佳答案

您需要获取所有 <div>首先包含输入的元素,然后是 None 时的元素复选框被勾选,你调用每个 <div>根据需要淡出。

<html>
<title>
</title>
<head>
</head>
<body>
<!-- Multiple Checkboxes (inline) -->
<div class="form-group">


<label class="col-md-4 control-label" for="checkboxes">
Select any desired backgrounds:</label>
<div class="col-md-4">
<label class="checkbox-inline" for="checkboxes-0">
<input name="checkboxes" id="checkboxes-0" type="checkbox" value="blanks">
Blanks
</label>
<label class="checkbox-inline" for="checkboxes-1">
<input name="checkboxes" id="checkboxes-1" type="checkbox" value="fresh">
Fresh Water
</label>
<label class="checkbox-inline" for="checkboxes-2">
<input name="checkboxes" id="checkboxes-2" type="checkbox" value="marine">
Marine Water
</label>
<label class="checkbox-inline" for="checkboxes-3">
<input name="checkboxes" id="checkboxes-3" type="checkbox" value="rain">
Rain
</label>
<label class="checkbox-inline" for="checkboxes-4">
<input name="checkboxes" id="checkboxes-4" type="checkbox" value="none">
None
</label>
</div>
</div>

<!-- Text input-->
<div id="blank_co" div class="form-group">
<label class="col-md-4 control-label" for="blankbg">Blanks cutoff: (1-99)</label>
<div class="col-md-4">
<input name="blankbg" class="form-control input-md" id="blankbg" required="" type="text" placeholder="" value="10">

</div>
</div>

<!-- Text input-->
<div id="fresh_co" div class="form-group">
<label class="col-md-4 control-label" for="freshbg">Fresh water cutoff: (1-99)</label>
<div class="col-md-4">
<input name="freshbg" class="form-control input-md" id="freshbg" required="" type="text" placeholder="" value="10">

</div>
</div>

<!-- Text input-->
<div id="marine_co" div class="form-group">
<label class="col-md-4 control-label" for="marinebg">Marine water cutoff: (1-99)</label>
<div class="col-md-4">
<input name="marinebg" class="form-control input-md" id="marinebg" required="" type="text" placeholder="" value="10">

</div>
</div>

<!-- Text input-->
<div id="rain_co" div class="form-group">
<label class="col-md-4 control-label" for="rainbg">Rain water cutoff: (1-99)</label>
<div class="col-md-4">
<input name="rainbg" class="form-control input-md" id="rainbg" required="" type="text" placeholder="" value="10">

</div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#checkboxes-0').change(function(){
if(this.checked)
$('#blank_co').fadeIn('slow');
else
$('#blank_co').fadeOut('slow');

});

$('#checkboxes-1').change(function(){
if(this.checked)
$('#fresh_co').fadeIn('slow');
else
$('#fresh_co').fadeOut('slow');

});

$('#checkboxes-2').change(function(){
if(this.checked)
$('#marine_co').fadeIn('slow');
else
$('#marine_co').fadeOut('slow');

});

$('#checkboxes-3').change(function(){
if(this.checked)
$('#rain_co').fadeIn('slow');
else
$('#rain_co').fadeOut('slow');
});

var $others = $('input[name="checkboxes"]').not('#checkboxes-4')
var $othersWrappers = $('div[id$="_co"]')
$('#checkboxes-4').change(function () {
if (this.checked) {
$others.prop('checked', false)
$othersWrappers.each(function( index ) {
$(this).fadeOut('slow');
});
}
});
$others.change(function () {
if (this.checked) {
$('#checkboxes-4').prop('checked', false)
}
})


});
</script>
</body>

</html>

关于javascript - Jquery 隐藏/取消隐藏复选框更改未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com