MooTools 具有 Element.checked 属性,用于查看指定元素是否设置为“已检查”。 MooTools 还允许您在选择器中添加 ":checked" 以仅获取匹配的选定元素。这意味着您可以执行以下操作来确定是否选中了任何复选框:
if ($$("input[type=checkbox]:checked").length > 0)
或者这样确定no复选框当前被选中:
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 属性——比如,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");
}
});
});