最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Grabbing the value of checkbox in Vue - Stack Overflow

programmeradmin1浏览0评论

Hey so I'm currently a newbie in Vue so have some mercy :)

I am trying to implement a filter bar, where I want to render the checked-box onto a collection view. So a use case would be, if T-shirts was checked then that should render all T-shirts from a DB.

So currently I'm grabbing all the values of beers from my database and rendering that into my filter box:

  <div class="overflow menu-list">
    <ul>
      <li v-for="brewery in breweryName">
        <input type="checkbox" name="beer">{{brewery}}
      </li>
    </ul>
  </div>

Where my Vue instance is defined as so:

var filterVM = new Vue({
  el: '#filter-bar',
  data : {
    breweryName : grabFromBeerDB("brewery"),
    beerStyle : grabFromBeerDB("style"),
    checkedBrewery : []
  },
  firebase : {
    beerList: beerDatabaseRef
  }, ...

My only question here is how would I reactively grab the values of the boxes that are checked?

Thanks for the help

Hey so I'm currently a newbie in Vue so have some mercy :)

I am trying to implement a filter bar, where I want to render the checked-box onto a collection view. So a use case would be, if T-shirts was checked then that should render all T-shirts from a DB.

So currently I'm grabbing all the values of beers from my database and rendering that into my filter box:

  <div class="overflow menu-list">
    <ul>
      <li v-for="brewery in breweryName">
        <input type="checkbox" name="beer">{{brewery}}
      </li>
    </ul>
  </div>

Where my Vue instance is defined as so:

var filterVM = new Vue({
  el: '#filter-bar',
  data : {
    breweryName : grabFromBeerDB("brewery"),
    beerStyle : grabFromBeerDB("style"),
    checkedBrewery : []
  },
  firebase : {
    beerList: beerDatabaseRef
  }, ...

My only question here is how would I reactively grab the values of the boxes that are checked?

Thanks for the help

Share asked Mar 14, 2017 at 5:11 lobcity42lobcity42 1091 gold badge2 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Just use v-model

<input type="checkbox" name="beer" value="A" v-model="checkedBrewery">
<input type="checkbox" name="beer" value="B" v-model="checkedBrewery">
<input type="checkbox" name="beer" value="C" v-model="checkedBrewery">

docs: https://v2.vuejs/v2/guide/forms.html#Checkbox

    <li v-for="brewery in breweryName">
        <input type="checkbox" name="beer" :value="brewery" v-model="checkedBrewery">
       {{brewery}}
    </li>

The value of input is required, and you must make sure each value is different with others.

发布评论

评论列表(0)

  1. 暂无评论