HTML checkbox onclick called in Javascript(在 Javascript 中调用 HTML 复选框 onclick)
问题描述
我在试图弄清楚如何让我的代码的某个部分工作时遇到了一些麻烦.
I am having a bit of trouble trying to figure out how to get a certain part of my code to work.
<input type="checkbox" id="check_all_1" name="check_all_1" title="U2VsZWN0IEFsbA==" onclick="selectAll(document.wizard_form, this);">
<label for="check_all_1" onclick="toggleCheckbox('check_all_1'); return false;">Select All</label>
这是我的 HTML,它可以正常工作(单击文本将单击框).它的 javascript 非常简单:
This is my HTML which works as it should (clicking the text will click the box). The javascript for it is pretty simple:
function toggleCheckbox(id) {
document.getElementById(id).checked = !document.getElementById(id).checked;
}
但是,当标签是使复选框被单击的原因时,我希望输入发生 onclick.目前,onClick js 没有运行.关于如何做到这一点的一个建议是什么?我试图将输入的 onclick 添加到标签的 onclick 中,但这不起作用.
However I want the onclick to happen for the input when the label is what makes the checkbox to be clicked. At this current time the onClick js does not go. What is one suggestion on how to do this? I tried to add the onclick of the input to the onclick of the label but that doesn't work.
任何建议/解决方案都会很棒.
Any suggestions/solutions would be wonderful.
推荐答案
如何把checkbox
放入 label
,制作标签自动为复选框点击敏感",并为复选框提供 onchange
事件?
How about putting the checkbox
into the label
, making the label automatically "click sensitive" for the check box, and giving the checkbox a onchange
event?
<label ..... ><input type="checkbox" onchange="toggleCheckbox(this)" .....>
function toggleCheckbox(element)
{
element.checked = !element.checked;
}
这将另外捕获用户使用键盘切换复选框,而 onclick
不会.
This will additionally catch users using a keyboard to toggle the check box, something onclick
would not.
这篇关于在 Javascript 中调用 HTML 复选框 onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Javascript 中调用 HTML 复选框 onclick


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01