Check all checkbox by class name.

huangapple go评论98阅读模式
英文:

check all checkbox by class name

问题

Here's the translated code you provided:

我试图使用JavaScript和Vue来检查和取消选中所有复选框

const checkAll = () => {
    var array = document.getElementsByClassName("assign_register");
    for (var i = 0; i < array.length; i++) {
        if (array[i].type == "checkbox") {
            if (array[i].className == "assign_register") {
                array[i].checked = true;
            } else if (array[i].checked) {
                array[i].checked = false;
            }
        }
    }
}

这个函数从我的主复选框调用

<template v-for="item in valor" :key="item.id">
    <tr>
        <td><input type="checkbox" class="assign_register" name="assign_register" style="width: 20px; height: 20px;"></td>
    </tr>
</template>

我想要的是当我点击这个复选框时检查所有子复选框如果它被选中并取消选中此复选框则取消选中所有复选框我尝试使用事件

const checkAll = () => {
    var array = document.getElementsByClassName("assign_register");
    for (var i = 0; i < array.length; i++) {
        array[i].addEventListener('change', e => {
            if (e.target.checked === true) {
                console.log("复选框已选中 - 布尔值: ", e.target.checked)
            }
            if (e.target.checked === false) {
                console.log("复选框未选中 - 布尔值: ", e.target.checked)
            }
        });
    }
}

但是不会在控制台中显示任何内容也不会检查我的输入...

**更新**

const checkAll = () => {
    var array = document.getElementsByClassName("assign_register");
    for (var i = 0; i < array.length; i++) {
        if (array[i].type == "checkbox") {
            if (array[i].className == "assign_register") {
                array[i].checked = true;
                if (array[i].checked == true) {
                    array[i].checked = false;
                }
            }
        }
    }
}

**更新2**

<td><input type="checkbox" class="assign_register" name="assign_register" v-model="checked" style="width: 20px; height: 20px;"></td>

const checkAll = () => {
    return {
        checked: true
    }
}

谢谢阅读对不好的英语感到抱歉

Please note that the code provided seems to have some issues, and it might not behave as expected. You may need to further debug and modify it to achieve your desired functionality.

英文:

I´m trying to check and unchex all check boxes with javascript and vue.

const checkAll = () =&gt; {
var array = document.getElementsByClassName(&quot;assign_register&quot;);
for(var i = 0; i &lt; array.length; i++){
if(array[i].type == &quot;checkbox&quot;){
if(array[i].className == &quot;assign_register&quot;){
array[i].checked = true;
}else if(array[i].checked){
array[i].checked = false;
}
}
}
}

This function it´s called from my principal checkbox:

&lt;template v-for=&quot;item in valor&quot; :key=&quot;item.id&quot;&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;checkbox&quot; class=&quot;assign_register&quot; name=&quot;assign_register&quot; style=&quot;width: 20px; height: 20px;&quot;&gt;&lt;/td&gt;

i want to do, that when i click in this check box check all my child checkboxes and if it´s checked and uncheck this check box, unchecked all. i tryed with event.

const checkAll = () =&gt; {
var array = document.getElementsByClassName(&quot;assign_register&quot;);
for(var i = 0; i &lt; array.length; i++){
array[i].addEventListener(&#39;change&#39;, e =&gt; {
if(e.target.checked === true) {
console.log(&quot;Checkbox is checked - boolean value: &quot;, e.target.checked)
}
if(e.target.checked === false) {
console.log(&quot;Checkbox is not checked - boolean value: &quot;, e.target.checked)
}
});
}
}

but not show anything in console and not checked my inputs...

UPDATE

const checkAll = () =&gt; {
var array = document.getElementsByClassName(&quot;assign_register&quot;);
for(var i = 0; i &lt; array.length; i++){
if(array[i].type == &quot;checkbox&quot;){
if(array[i].className == &quot;assign_register&quot;){
array[i].checked = true;
if(array[i].checked == true){
array[i].checked = false;
}
}
}
}
}

UPDATE 2

&lt;td&gt;&lt;input type=&quot;checkbox&quot; class=&quot;assign_register&quot; name=&quot;assign_register&quot; v-model=&quot;checked&quot; style=&quot;width: 20px; height: 20px;&quot;&gt;&lt;/td&gt;
const checkAll = () =&gt; {
return {
checked: true
}
}

Thanks for readme and sorry for my bad english

答案1

得分: 2

I didn't understand your question very well, but if I assume you want to add a parent element when changing its state, you want all child elements to have the same state.

<div>
  Main Check Box: <input type="checkbox" class="main_checkbox" /> Change Me To Change All Other Checkboxes<br />
  <hr />
  Child 1: <input type="checkbox" class="assign_register" /> <br />
  Child 2: <input type="checkbox" class="assign_register" /> <br />
  Child 3: <input type="checkbox" class="assign_register" /> <br />
  Child 4: <input type="checkbox" checked class="assign_register" /> <br />
</div>
英文:

I didn't understand your question very well, but if I assume you want to add a parent element when changing its state, you want all child elements to have the same state

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&#39;.main_checkbox&#39;).addEventListener(&quot;change&quot;,function(){
let isChecked=this.checked;
document.querySelectorAll(&quot;input[type=&#39;checkbox&#39;].assign_register&quot;).forEach((chk)=&gt;{
chk.checked=isChecked;
})
})

<!-- language: lang-html -->

&lt;div&gt;
Main Check Box:&lt;input type=&quot;checkbox&quot; class=&quot;main_checkbox&quot; /&gt; Change Me To Change All Other CheckBoxes&lt;br/&gt;
&lt;hr/&gt;
Child 1:&lt;input type=&quot;checkbox&quot; class=&quot;assign_register&quot; /&gt; &lt;br/&gt;
Child 2:&lt;input type=&quot;checkbox&quot; class=&quot;assign_register&quot; /&gt; &lt;br/&gt;
Child 3:&lt;input type=&quot;checkbox&quot; class=&quot;assign_register&quot; /&gt; &lt;br/&gt;
Child 4:&lt;input type=&quot;checkbox&quot; checked class=&quot;assign_register&quot; /&gt; &lt;br/&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

为每个valor项目创建一个字段,例如item.checked,并将其放入v-model中,类似于这样

顺便说一句,在vue.js中尽量避免使用vanilla.js选择器,这是该框架存在的最基本原因。

> 在处理前端表单时,我们经常需要将表单输入元素的状态与JavaScript中相应的状态同步。手动设置值绑定和更改事件监听器可能很麻烦。

英文:

Make a field for each valor item like item.checked and put it in v-model like this

BTW avoid using vanilla.js selections in vue.js by any means, this is the most basic reason that such framework exist.

> When dealing with forms on the frontend, we often need to sync the state of form input elements with corresponding state in JavaScript. It can be cumbersome to manually wire up value bindings and change event listeners

答案3

得分: 0

It appears that it never enters the "else if" block because the "if" condition is always true since you are selecting elements with the "assign_register" className. Is there anything else you would like to know about this code?

英文:
const checkAll = () =&gt; {
var array = document.getElementsByClassName(&quot;assign_register&quot;);
for(var i = 0; i &lt; array.length; i++){
if(array[i].type == &quot;checkbox&quot;){
if(array[i].className == &quot;assign_register&quot;){
array[i].checked = true;
}else if(array[i].checked){
array[i].checked = false;
}
}
}
}

It seems like it never goes to else if block because if is always true, as you get only elements with that className. Or am I missing something?

huangapple
  • 本文由 发表于 2023年4月17日 14:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定