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

javascript - VueJS Checkbox Model Array of Ints - Stack Overflow

programmeradmin1浏览0评论

I have an issue with VueJS 1.0, and this behavior works in VueJS 2.0. In VueJS 1.0 if I have a list of integers and have a checkbox v-model bound to it, the list of integers will not map as a checked attribute.

Here is the HTML

<div id="app">
  <div class="col-sm-offset-3 col-sm-4 clearfix text-center">
    <h4>On Each Day of The Week</h4>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox1" v-model="schedules[0].by_days" value="1"> M
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox2" v-model="schedules[0].by_days" value="2"> Tu
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox3" v-model="schedules[0].by_days" value="3"> W
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox4" v-model="schedules[0].by_days" value="4"> Th
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox5" v-model="schedules[0].by_days" value="5"> F
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox6" v-model="schedules[0].by_days" value="6"> Sa
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox7" v-model="schedules[0].by_days" value="7"> Su
    </label>
    <div class="clearfix"></div>
  </div>
  By Days: {{ schedules[0].by_days.join(', ') }}
</div>

Then here is the JavaScript:

new Vue({
    el: '#app',
    data: {
        schedules: [
            {
            'by_days': ["1", 2, 3]
          }
        ]
    }
})

The output will have "1" correctly checkboxed, but 2 & 3 are integers and will not show as checked. In VueJS 2.0 this works, but not VueJS 1.0.

A full JSFiddle is available here: /

I have an issue with VueJS 1.0, and this behavior works in VueJS 2.0. In VueJS 1.0 if I have a list of integers and have a checkbox v-model bound to it, the list of integers will not map as a checked attribute.

Here is the HTML

<div id="app">
  <div class="col-sm-offset-3 col-sm-4 clearfix text-center">
    <h4>On Each Day of The Week</h4>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox1" v-model="schedules[0].by_days" value="1"> M
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox2" v-model="schedules[0].by_days" value="2"> Tu
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox3" v-model="schedules[0].by_days" value="3"> W
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox4" v-model="schedules[0].by_days" value="4"> Th
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox5" v-model="schedules[0].by_days" value="5"> F
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox6" v-model="schedules[0].by_days" value="6"> Sa
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="inlineCheckbox7" v-model="schedules[0].by_days" value="7"> Su
    </label>
    <div class="clearfix"></div>
  </div>
  By Days: {{ schedules[0].by_days.join(', ') }}
</div>

Then here is the JavaScript:

new Vue({
    el: '#app',
    data: {
        schedules: [
            {
            'by_days': ["1", 2, 3]
          }
        ]
    }
})

The output will have "1" correctly checkboxed, but 2 & 3 are integers and will not show as checked. In VueJS 2.0 this works, but not VueJS 1.0.

A full JSFiddle is available here: https://jsfiddle/qf5gqsg6/

Share Improve this question edited Nov 7, 2018 at 11:20 Xquick 6771 gold badge8 silver badges20 bronze badges asked Oct 29, 2016 at 10:19 Joseph MontanezJoseph Montanez 1,7081 gold badge20 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Change your data ["1",2,3] into [1,2,3]

Change your your checkbox input element value into :value

I found the answer, I need to set the bind the value to the input instead of just relying on the value from the input.

So instead of:

<input type="checkbox" v-model="schedules[0].by_days" value="2"> M

It needed to be:

<input type="checkbox" v-model="schedules[0].by_days" v-bind:value="2"> M

Of course this doesn't work if there is a list of mixed strings and integer numbers, but it works in my case where everything was an integer.

发布评论

评论列表(0)

  1. 暂无评论