HTML 复选框 onclick 在 Javascript 中调用

发布于 2024-09-08 05:48:07 字数 643 浏览 1 评论 0原文

我在尝试弄清楚如何使我的代码的某个部分正常工作时遇到了一些麻烦。

<input type="checkbox" id="check_all_1" name="check_all_1" title="Select All" onclick="selectAll(document.wizard_form, this);">
<label for="check_all_1" onclick="toggleCheckbox('check_all_1'); return false;">Select All</label>

这是我的 HTML,它可以正常工作(单击文本将单击该框)。它的 javascript 非常简单:

function toggleCheckbox(id) {
    document.getElementById(id).checked = !document.getElementById(id).checked;
}

但是,当标签使复选框被单击时,我希望输入发生 onclick。目前 onClick js 还没有运行。关于如何做到这一点的建议是什么? 我尝试将输入的 onclick 添加到标签的 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="Select All" onclick="selectAll(document.wizard_form, this);">
<label for="check_all_1" onclick="toggleCheckbox('check_all_1'); return false;">Select All</label>

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;
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那请放手 2024-09-15 05:48:07

如何将复选框放入标签中,使标签自动为复选框“点击敏感”,并为复选框指定一个onchange 事件?

<label ..... ><input type="checkbox" onchange="toggleCheckbox(this)" .....> 

function toggleCheckbox(element)
 {
   element.checked = !element.checked;
 }

这还会捕获用户使用键盘来切换复选框,而 onclick 则不会。

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;
 }

This will additionally catch users using a keyboard to toggle the check box, something onclick would not.

享受孤独 2024-09-15 05:48:07

没有 onclick 的标签将按照您的预期运行。它改变了输入。您真正想要的是在单击标签时执行 selectAll(),对吗?
然后只将select all添加到onclick标签上。或者将输入包装到标签中并仅为标签分配 onclick

<label for="check_all_1" onclick="selectAll(document.wizard_form, this);">
  <input type="checkbox" id="check_all_1" name="check_all_1" title="Select All">
  Select All
</label>

Label without an onclick will behave as you would expect. It changes the input. What you relly want is to execute selectAll() when you click on a label, right?
Then only add select all to the label onclick. Or wrap the input into the the label and assign onclick only for the label

<label for="check_all_1" onclick="selectAll(document.wizard_form, this);">
  <input type="checkbox" id="check_all_1" name="check_all_1" title="Select All">
  Select All
</label>
蘑菇王子 2024-09-15 05:48:07

jQuery 有一个函数可以做到这一点:

  1. 在你的头脑中包含以下脚本:

    <脚本类型=“text/javascript”src=“http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js”>
    

    (或者只是在线下载 jQuery.js 文件并将其包含在本地)

  2. 使用此单击输入时切换复选框的脚本:

    var 切换 = false;
    $("#INPUTNAMEHERE").click(function() {
            $("input[type=checkbox]").attr("选中",!toggle);
            切换=!切换;
    }); 
    

如果我理解您想要执行的操作,那么应该可以执行您想要的操作。

jQuery has a function that can do this:

  1. include the following script in your head:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    

    (or just download the jQuery.js file online and include it locally)

  2. use this script to toggle the check box when the input is clicked:

    var toggle = false;
    $("#INPUTNAMEHERE").click(function() {
            $("input[type=checkbox]").attr("checked",!toggle);
            toggle = !toggle;
    }); 
    

That should do what you want if I understood what you were trying to do.

沉鱼一梦 2024-09-15 05:48:07

您还可以从 HTML 中提取事件代码,如下所示:

<input type="checkbox" id="check_all_1" name="check_all_1" title="Select All" />
<label for="check_all_1">Select All</label>

<script>
function selectAll(frmElement, chkElement) {
    // ...
}
document.getElementById("check_all_1").onclick = function() {
    selectAll(document.wizard_form, this);
}
</script>

You can also extract the event code from the HTML, like this :

<input type="checkbox" id="check_all_1" name="check_all_1" title="Select All" />
<label for="check_all_1">Select All</label>

<script>
function selectAll(frmElement, chkElement) {
    // ...
}
document.getElementById("check_all_1").onclick = function() {
    selectAll(document.wizard_form, this);
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文