【问题标题】:In Vue, how to cancel a checkbox based on a condition?在Vue中,如何根据条件取消复选框?
【发布时间】:2018-03-22 03:43:25
【问题描述】:

我希望始终至少选中一个复选框,但我混合了 v-model:checked 的概念。

doc 说:

v-model 将忽略初始的 valuecheckedselected 属性 在任何表单元素上都可以找到。它将始终处理 Vue 实例数据 作为真理的源泉。

我可以阻止我的模型被修改,但我不能阻止复选框被选中...

一些代码:

模板

<div class="wrapper" v-for="(s, id) in userOutputSeries" :key="id">
  <input type="checkbox" :id="id" :value="id" @change="preventIfLessThanOneChecked" :checked="s.active">
  <label :for="id">{{ s.display }}</label>
</div>

模特userOutputSeries

data () {
  return {
    userOutputSeries: {
      foo: {
        display: 'Awesome Foo',
        active: true
      },
      bar: {
        display: 'My Bar',
        active: false
      }
    }
  }
}

preventIfLessThanOneChecked 处理程序

preventIfLessThanOneChecked (event) {
  // I don't update the model so it stay at the same state
  // But only need to count the active series and do what we want.
  console.log(event.target.value, event.target.checked)
}

有什么想法可以阻止本机复选框的传播吗?

【问题讨论】:

    标签: checkbox vue.js vuejs2 v-model


    【解决方案1】:

    尝试在单个选中的复选框上使用disabled

    <div class="wrapper" v-for="(s, id) in userOutputSeries" :key="id">
      <input type="checkbox" :id="id" :value="id"
          :disabled="(s.active && numberOfChecked == 1) ? disabled : null"
          @change="preventIfLessThanOneChecked" :checked="s.active">
      <label :for="id">{{ s.display }}</label>
    </div>
    

    【讨论】:

    • 我将结合使用 disabled 属性和@thanksd 答案来添加一些额外的样式。您的解决方案也有效。谢谢!
    【解决方案2】:

    您应该使用v-model 而不是:checked,以便对userOutputSeries 数据属性的更改将反映在复选框输入中。

    然后,将v-for 中的s 引用传递给该方法,如果没有active 复选框,则将该对象的active 属性设置为true

    new Vue({
      el: '#app',
      data() {
        return {
          userOutputSeries: {
            foo: {
              display: 'Awesome Foo',
              active: true
            },
            bar: {
              display: 'My Bar',
              active: false
            }
          }
        }
      },
      methods: {
        preventIfLessThanOneChecked(item) {
          if (item.active) {
            return;
          }
        
          let items = Object.values(this.userOutputSeries);
          if (!items.find(i => i.active)) {
            item.active = true;
          }
        }
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
    <div id="app">
      <div class="wrapper" v-for="(s, id) in userOutputSeries" :key="id">
        <input type="checkbox" :id="id" :value="id" @change="preventIfLessThanOneChecked(s)" v-model="s.active">
        <label :for="id">{{ s.display }}</label>
      </div>
    </div>

    【讨论】:

    • 我一直在阻止事件而不是考虑数据...谢谢!
    【解决方案3】:

    在@thanksd 给出的上述答案中,我的复选框仍未选中。 所以我正在写我的解决方案。

    这是我的循环语句,根据你的文件更改变量名。

    v-for="column in tableColumns"
    

    这是我的输入(如果可见为真,则选中复选框)

    <input type="checkbox" v-model="column.visible" @change="event => visibleColumnsChanged(column, event)">
    

    然后在我的更改方法中 - 如果没有可见项目,则将 column.visible 设置为 true - 使用 event.target.checked = true 再次选中复选框。

    visibleColumnsChanged: function(column, event){
      if (column.visible) {
        return;
      }
    
      if(! this.tableColumns.find(c => c.visible)){
        column.visible = true;
    
        event.target.checked = true;
      }
    }
    

    【讨论】:

    • Tnx 很多,这应该是被接受的答案,因为当做取消检查时,但随后将 v-model 值务实地更改回 "true" ,复选框将忽略这一点并保持未选中状态,即使认为模型实际上是真的。当您希望始终未选中一个复选框时,可能会发生这种行为。只有这个“event.target.checked”魔法实际上对 tnx 有很大帮助。另一种解决方法是在最后一次未选中时实际隐藏复选框,因此用户也不能这样做。
    • 很高兴听到它对@JiroMatchonson 有所帮助
    猜你喜欢
    • 2017-05-20
    • 2019-01-03
    • 2020-08-30
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多