MooTools具有属性Element.checked以查看指定元素是否设置为“checked”或不是。 MooTools还允许您将“:checked”添加到您的选择器以仅获取匹配的选定元素。这意味着你可以做这样的事情,以确定任何复选框是否被选中:
if ($$("input[type=checkbox]:checked").length > 0)
还是这个确定没有复选框正在检查:
if ($$("input[type=checkbox]:checked").length < 1)
给你挂样表,这里有一种方法:
$$("input[type=checkbox]").each(function(checkboxInput) {
checkboxInput.addEvent("click", function() {
// show submit button if at least 1 checkbox is checked
if (checkboxInput.checked) {
$$("input[type=submit]").each(function(submitButton) {
submitButton.removeProperty("disabled");
});
}
// hide submit button if no checkboxes are checked
if ($$("input[type=checkbox]:checked").length < 1) {
$$("input[type=submit]").each(function(submitButton) {
submitButton.set("disabled", "disabled");
});
}
});
});
如果你的提交按钮有一个id属性 - say,id =“formSubmitButton”你可以通过这种方式引用它,并使用稍少的代码完成同样的事情,而不必使用$$(“input [type = submit]”)。each(function(submitButton){...}:
$("formSubmitButton").removeProperty("disabled");
要从禁用/重新启用按钮变为显示/隐藏按钮,您可以使用属性style =“display:none;”设置按钮来启动窗体,然后调用submitButton.removeProperty(“style” )复选框被选中时,submitButton.setStyle(“display”,“none”)不再被选中。该版本的完整代码示例,使用提交按钮ID:
$$("input[type=checkbox]").each(function(checkboxInput) {
checkboxInput.addEvent("click", function() {
// show submit button if at least 1 checkbox is checked
if (checkboxInput.checked) {
$("formSubmitButton").removeProperty("style");
}
// hide submit button if no checkboxes are checked
if ($$("input[type=checkbox]:checked").length < 1) {
$("formSubmitButton").setStyle("display", "none");
}
});
});