如何解决复选框动态选择重力形式
我有一个复选框,想说这个复选框:如果(例如)选择了“Einzelberatung BU”,并且您想选择另一个带有“Einzelberatung...”一词的字段,则应自动选择字段“Komplettes Finanzkonzept”并且应该取消选择其他两个字段。
如您所见,我是 JavaScript 的绝对初学者。所以希望有人能帮我解决下面粘贴的代码,并告诉我我做错了什么。
感谢您的时间, 莱昂
bar
jQuery('#input_167_1 input[type="checkbox"]').on('click',function() {
var $bu = jQuery('#choice_2_9_1');
var $pkv = jQuery('#choice_2_9_2');
var $etf = jQuery('#choice_2_9_3');
var $fiko = jQuery('#choice_2_9_4');
// Check Green if Rea & Blue are checked.
if ($bu.is(':checked') && $etf.is(':checked')) {
$bu.prop('checked',false);
$pkv.prop('checked',false);
$etf.prop('checked',false);
$fiko.prop('checked',true);
} else if ($bu.is(':checked') && $pkv.is(':checked')) {
$bu.prop('checked',true);
} else if ($pkv.is(':checked') && $etf.is(':checked')) {
$bu.prop('checked',true);
}
// Prevent Red & Blue from being checked if Green is checked.
else if ($fiko.is(':checked')) {
$bu.prop('checked',false);
}
});
解决方法
Tyr 下面的代码。我删除了 #input_167_1
并将 click
更改为 change
。
jQuery('input[type="checkbox"]').on('change',function() {
var $bu = jQuery('#choice_2_9_1');
var $pkv = jQuery('#choice_2_9_2');
var $etf = jQuery('#choice_2_9_3');
var $fiko = jQuery('#choice_2_9_4');
// Check Green if Rea & Blue are checked.
if ($bu.is(':checked') && $etf.is(':checked')) {
$bu.prop('checked',false);
$pkv.prop('checked',false);
$etf.prop('checked',false);
$fiko.prop('checked',true);
} else if ($bu.is(':checked') && $pkv.is(':checked')) {
$bu.prop('checked',true);
} else if ($pkv.is(':checked') && $etf.is(':checked')) {
$bu.prop('checked',true);
}
// Prevent Red & Blue from being checked if Green is checked.
else if ($fiko.is(':checked')) {
$bu.prop('checked',false);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="gfield_checkbox" id="input_2_9">
<li class="gchoice_2_9_1">
<input name="input_9.1" type="checkbox" value="Einzelberatung BU" id="choice_2_9_1">
<label for="choice_2_9_1" id="label_2_9_1">Einzelberatung BU</label>
</li><li class="gchoice_2_9_2">
<input name="input_9.2" type="checkbox" value="Einzelberatung PKV" id="choice_2_9_2">
<label for="choice_2_9_2" id="label_2_9_2">Einzelberatung PKV</label>
</li><li class="gchoice_2_9_3">
<input name="input_9.3" type="checkbox" value="Einzelberatung ETF Rente" id="choice_2_9_3">
<label for="choice_2_9_3" id="label_2_9_3">Einzelberatung ETF Rente</label>
</li><li class="gchoice_2_9_4">
<input name="input_9.4" type="checkbox" value="investmentplanung" id="choice_2_9_4">
<label for="choice_2_9_4" id="label_2_9_4">investmentplanung</label>
</li><li class="gchoice_2_9_5">
<input name="input_9.5" type="checkbox" value="Komplettes Finanzkonzept" id="choice_2_9_5">
<label for="choice_2_9_5" id="label_2_9_5">Komplettes Finanzkonzept</label>
</li><li class="gchoice_2_9_6">
<input name="input_9.6" type="checkbox" value="Anderese Anliegen" id="choice_2_9_6">
<label for="choice_2_9_6" id="label_2_9_6">Anderese Anliegen</label>
</li>
</ul>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。