我想在取消选中 1 个或两个复选框时禁用按钮,因此仅在选中两个复选框时才启用它。
复选框是在页面加载后添加的,因此我的代码当前如下所示:
jQuery(document).on('change','#voorwaarden', function() {
jQuery("#checkoutbtn").prop('disabled', !this.checked)
});
还有我的 html:
<button type="submit" title="<?php echo $this->__('Place Order') ?>" disabled="disabled" class="button btn-checkout" id="checkoutbtn" onclick="review.save();"><span><?php echo $this->__('Place Order') ?></span></button>
<p><input type="checkbox" class="checkbox" id="voorwaarden" style="display:inline;" required/><b> Ik ga akkoord met de algemene voorwaarden <em>*</em></b></p>
<p><input type="checkbox" class="checkbox" id="geboorte" style="display:inline;"/><b> Ik ben ouder dan 18 jaar <em>*</em></b></p>
还有一个问题,我想在单击按钮和禁用按钮时显示警报,但我发现无法在禁用的按钮上触发单击功能。
目前,仅当单击 #voorwaarden
时才会启用该按钮。
最佳答案
这样试试
$(document).on('change','.chk', function() {
var checkCount=$('.chk:checked').length;
var totalCheckbox =$('.chk').length;
totalCheckbox==checkCount ? $("#checkoutbtn").prop('disabled', false):$("#checkoutbtn").prop('disabled', true);
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
</head>
<body>
<button type="submit" title="<?php echo $this->__('Place Order') ?>" disabled="disabled" class="button btn-checkout" id="checkoutbtn" onclick="review.save();"><span><?php echo $this->__('Place Order') ?></span></button>
<p><input type="checkbox" class="checkbox chk" id="voorwaarden" style="display:inline;" required/><b> Ik ga akkoord met de algemene voorwaarden <em>*</em></b></p>
<p><input type="checkbox" class="checkbox chk" id="geboorte" style="display:inline;"/><b> Ik ben ouder dan 18 jaar <em>*</em></b></p>
</body>
</html>
关于javascript - 检查两个复选框是否未选中并禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45978441/