0

I have 2 radio buttons. I need to bind their value to a variable by using v-model in vue.js. When v-model is added, both radio buttons showing as unchecked.

<div class="radio-item">
  <input type="radio" id="ritema" name="ritem" value="business" checked v-model="picked">
  <label for="ritema">Business </label>
</div>

<div class="radio-item">
    <input type="radio" id="ritemb" name="ritem" value="public" v-model="picked">
    <label for="ritemb">Public</label>
</div>
3
  • Did you set picked data value to either "public" or "business"? Commented May 9, 2017 at 6:06
  • No..I didn't. So we should set value to 'picked' in the data section right? Commented May 9, 2017 at 6:12
  • That works....Thank you Commented May 9, 2017 at 6:14

1 Answer 1

2

v-model always use the data property as the single source of truth.

v-model will ignore the initial value, checked or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.

Source: https://v2.vuejs.org/v2/guide/forms.html#Basic-Usage

So as the comment said, you just have to work on the value of picked on your Vue instance.

data() {
    return {
        picked: "business"
    };
}
Sign up to request clarification or add additional context in comments.

2 Comments

is there any way to vue not ignore the default value from html?
It may be possible to assign the value in the mounted hook: get it directly from the DOM, using the ref attribute. See: vuejs.org/v2/api/#ref Not tested by the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.