简体   繁体   English

在我手动选中和取消选中复选框后,如何使复选框响应 select all function?

[英]How do I make check-boxes respond to a select all function after I manually checked and unchecked them?

I managed to select all check-boxes by getting the input elements via class name and add or remove their "checked" attribute.我通过 class 名称获取输入元素并添加或删除它们的“已检查”属性,设法 select 所有复选框。 However, I've discovered that if I checked and unchecked any of the check-boxes, they are no longer affected when I try to select all of them.但是,我发现如果我选中并取消选中任何复选框,当我尝试 select 所有这些复选框时,它们不再受到影响。 For example, in the image below, I checked and unchecked the first 3 check-boxes.例如,在下图中,我选中并取消选中了前 3 个复选框。

UI 示例显示未选中或未选中的复选框。

Things I have did: 1. Inspecting the elements - don't see any issues because the checkbox elements' attributes still update despite not showing any changes on the page.我所做的事情: 1. 检查元素 - 没有看到任何问题,因为复选框元素的属性仍然更新,尽管页面上没有显示任何更改。 2. Console - no errors reported here. 2. 控制台 - 这里没有报告错误。

<table>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="checked"></th>
        <th>ID</th>
        <th>Course Title</th>
        <th>Price</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="course in courses" :key="course.id">
        <td><input type="checkbox" class="inputs"></td>
        <td>{{course.id}}</td>
        <td>{{course.title}}</td>
        <td>{{course.price}}</td>
      </tr>
    </tbody>
  </table>
export default {
  name: 'CourseTable',
  data () {
    return {
      courses: udemy.results,
      checked: false
    }
  },
  methods: {
    check (arr) {
      for(let i=0; i<arr.length; i++){
        arr[i].setAttribute("checked", "")
      }
    },
    uncheck (arr) {
      for(let i=0; i<arr.length; i++){
        arr[i].removeAttribute("checked")
      }
    }
  },
  watch: {
    checked: function () {
      var inputs = document.getElementsByClassName("inputs")
      if(this.checked){
        this.check(inputs)
      }else{
        this.uncheck(inputs)
      }
    }
  }
}

You can change this line arr[i].setAttribute("checked", "") to arr[i].checked = true您可以将此行arr[i].setAttribute("checked", "")更改为arr[i].checked = true

and arr[i].removeAttribute("checked") to arr[i].checked = falsearr[i].removeAttribute("checked")arr[i].checked = false

You can actually delete check and uncheck methods as well as the watch method.您实际上可以删除检查和取消检查方法以及监视方法。 Then change your input checked value.然后更改您输入的检查值。 Like this:像这样:

 new Vue({ el: "#app", data() { return { checked: false, courses: [{ id: "362328", title: "AWS", price: "179.99" }, { id: "625204", title: "The web", price: "179.99" }, { id: "567828", title: "Complete", price: "179.99" }, { id: "756150", title: "Angular", price: "179.99" }, { id: "950390", title: "Machine", price: "179.99" }, { id: "793796", title: "Excel", price: "179.99" } ] } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id='app'> <table> <thead> <tr> <th><input type="checkbox" v-model="checked"></th> <th>ID</th> <th>Course Title</th> <th>Price</th> </tr> </thead> <tbody> <tr v-for="course in courses":key="course.id"> <td><input type="checkbox" class="inputs":checked='checked'></td> <td>{{course.id}}</td> <td>{{course.title}}</td> <td>{{course.price}}</td> </tr> </tbody> </table> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM