我在这里使用jQuery-ui按钮。检查两个check-boxes
时我想要的是我需要激活按钮并删除类disabledButton
,如果没有选中(两者),我希望禁用按钮和addClass disabledButton
。我怎么能这样做?
这是我的jQuery方法:
$(".conditionCheck").buttonset();
$(".conditions").click(function () {
if ($(".conditions").is(':checked')) {
$(".paymentButton").removeAttr("disabled")
.removeClass("disabledButton");
}
if (!$(".conditions").is(':checked')) {
$(".paymentButton").attr("disabled")
.addClass("disabledButton");
}
});
答案 0 :(得分:2)
使用$('.conditions').not(':checked').length
获取未经检查的控件数量。如果此数字为0,则两者都已经过检查,因此您知道该怎么做。
$(".conditions").click(function () {
if ($('.conditions').not(':checked').length) {
$(".paymentButton").prop("disabled", true).addClass("disabledButton");
}
else {
$(".paymentButton").prop("disabled", false).removeClass("disabledButton");
}
});
我还更改了incorrect use of attr
以禁用prop
元素,这是合适的方法。
上述代码也可以更简洁地编写为
var allChecked = $('.conditions').not(':checked').length == 0;
$(".paymentButton").prop("disabled", !allChecked)
.toggleClass("disabledButton", !allChecked);