如何使用JavaScript检查/不检查复选框?
当前回答
对于单个检查,请尝试
myCheckBox.checked = 1 <input type="checkbox" id="myCheckBox">给她打电话
对于多重尝试
document.querySelectorAll(“.imChecked”)。forEach (c = > c.checked = 1) 买酒:<input type="checkbox" class="imChecked"><br> 播放流畅的爵士音乐:<input type="checkbox"><br> 剃须:<input type="checkbox" class="imChecked"><br>
其他回答
使用vanilla js:
//for one element:
document.querySelector('.myCheckBox').checked = true //will select the first matched element
document.querySelector('.myCheckBox').checked = false//will unselect the first matched element
//for multiple elements:
for (const checkbox of document.querySelectorAll('.myCheckBox')) {
//iterating over all matched elements
checkbox.checked = true //for selection
checkbox.checked = false //for unselection
}
我们可以这样选中一个粒子复选框,
$('id of the checkbox')[0].checked = true
然后取消检查,
$('id of the checkbox')[0].checked = false
function setCheckboxValue(checkbox,value) {
if (checkbox.checked!=value)
checkbox.click();
}
如果出于某种原因,你不想(或不能)在复选框元素上运行.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只是演示它是如何工作的。
对于单个检查,请尝试
myCheckBox.checked = 1 <input type="checkbox" id="myCheckBox">给她打电话
对于多重尝试
document.querySelectorAll(“.imChecked”)。forEach (c = > c.checked = 1) 买酒:<input type="checkbox" class="imChecked"><br> 播放流畅的爵士音乐:<input type="checkbox"><br> 剃须:<input type="checkbox" class="imChecked"><br>
推荐文章
- 如何删除和清除所有的本地存储数据
- 如何从给定的html字符串中删除前导和尾随空白?
- 如何为KnockoutJS调试模板绑定错误?
- 将一个二进制的NodeJS Buffer转换为JavaScript的ArrayBuffer
- 检测浏览器标签是否有焦点
- JavaScript在数组中
- 如何从JavaScript中使用正则表达式的字符串中剥离所有标点符号?
- 如何修改标签文本?
- 如何获得十六进制颜色值而不是RGB值?
- 字符串strip() JavaScript?
- 如何将一个标题转换为jQuery的URL段塞?
- 获取对象的第一个索引
- 我如何用javascript编程点击链接?
- JavaScript:客户端验证与服务器端验证
- React钩子:从回调中访问最新状态