英文:
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 = () => {
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;
}
}
}
}
This function it´s called from my principal checkbox:
<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>
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 = () => {
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("Checkbox is checked - boolean value: ", e.target.checked)
}
if(e.target.checked === false) {
console.log("Checkbox is not checked - boolean value: ", e.target.checked)
}
});
}
}
but not show anything in console and not checked my inputs...
UPDATE
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;
}
}
}
}
}
UPDATE 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
}
}
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('.main_checkbox').addEventListener("change",function(){
let isChecked=this.checked;
document.querySelectorAll("input[type='checkbox'].assign_register").forEach((chk)=>{
chk.checked=isChecked;
})
})
<!-- language: lang-html -->
<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>
<!-- 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 = () => {
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;
}
}
}
}
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论