如何使用JavaScript检查/不检查复选框?
当前回答
我们可以这样选中一个粒子复选框,
$('id of the checkbox')[0].checked = true
然后取消检查,
$('id of the checkbox')[0].checked = false
其他回答
如果出于某种原因,你不想(或不能)在复选框元素上运行.click(),你可以直接通过它的.checked属性(<input type="checkbox">的IDL属性)改变它的值。
请注意,这样做不会触发通常相关的事件(更改),因此您需要手动触发它,以获得与任何相关事件处理程序一起工作的完整解决方案。
这是一个原始javascript (ES6)的函数示例:
class ButtonCheck { constructor() { let ourCheckBox = null; this.ourCheckBox = document.querySelector('#checkboxID'); let checkBoxButton = null; this.checkBoxButton = document.querySelector('#checkboxID+button[aria-label="checkboxID"]'); let checkEvent = new Event('change'); this.checkBoxButton.addEventListener('click', function() { let checkBox = this.ourCheckBox; //toggle the checkbox: invert its state! checkBox.checked = !checkBox.checked; //let other things know the checkbox changed checkBox.dispatchEvent(checkEvent); }.bind(this), true); this.eventHandler = function(e) { document.querySelector('.checkboxfeedback').insertAdjacentHTML('beforeend', '<br />Event occurred on checkbox! Type: ' + e.type + ' checkbox state now: ' + this.ourCheckBox.checked); } //demonstration: we will see change events regardless of whether the checkbox is clicked or the button this.ourCheckBox.addEventListener('change', function(e) { this.eventHandler(e); }.bind(this), true); //demonstration: if we bind a click handler only to the checkbox, we only see clicks from the checkbox this.ourCheckBox.addEventListener('click', function(e) { this.eventHandler(e); }.bind(this), true); } } var init = function() { const checkIt = new ButtonCheck(); } if (document.readyState != 'loading') { init; } else { document.addEventListener('DOMContentLoaded', init); } <input type="checkbox" id="checkboxID" /> <button aria-label="checkboxID">Change the checkbox!</button> <div class="checkboxfeedback">No changes yet!</div>
如果您运行此程序并同时单击复选框和按钮,您应该会了解它是如何工作的。
注意,我使用了文档。querySelector的简洁/简单,但这可以很容易地构建出来,要么有一个给定的ID传递给构造函数,或者它可以应用于充当复选框的aria-labels的所有按钮(注意,我没有费心在按钮上设置一个ID,并给复选框一个aria- labledby,如果使用此方法,应该这样做)或任何其他方法来展开它。最后两个addeventlistener只是演示它是如何工作的。
<script type="text/javascript">
$(document).ready(function () {
$('.selecctall').click(function (event) {
if (this.checked) {
$('.checkbox1').each(function () {
this.checked = true;
});
} else {
$('.checkbox1').each(function () {
this.checked = false;
});
}
});
});
</script>
我想指出的是,将'checked'属性设置为非空字符串会导致选中框。
因此,如果您将'checked'属性设置为"false",则复选框将被选中。我必须将值设置为空字符串,null或布尔值false,以确保复选框不被选中。
Javascript:
// Check
document.getElementById("checkbox").checked = true;
// Uncheck
document.getElementById("checkbox").checked = false;
jQuery 1.6 (+):
// Check
$("#checkbox").prop("checked", true);
// Uncheck
$("#checkbox").prop("checked", false);
jQuery (1.5 -):
// Check
$("#checkbox").attr("checked", true);
// Uncheck
$("#checkbox").attr("checked", false);
我同意目前的答案,但在我的情况下,它不起作用,我希望这段代码在未来能帮助到某人:
// check
$('#checkbox_id').click()
推荐文章
- 如何删除和清除所有的本地存储数据
- 如何从给定的html字符串中删除前导和尾随空白?
- 如何为KnockoutJS调试模板绑定错误?
- 将一个二进制的NodeJS Buffer转换为JavaScript的ArrayBuffer
- 检测浏览器标签是否有焦点
- JavaScript在数组中
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 如何修改标签文本?
- 如何获得十六进制颜色值而不是RGB值?
- 字符串strip() JavaScript?
- 如何将一个标题转换为jQuery的URL段塞?
- 获取对象的第一个索引
- 我如何用javascript编程点击链接?
- JavaScript:客户端验证与服务器端验证
- React钩子:从回调中访问最新状态