Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJs Checked Radio Button group

Tags:

vue.js

I am using vueJs and have a radio button group. When the radio is checked how can I bind a css border attribute to a class :class="selected"?

The :checked attribute does not work as I have bound it to the v-model.

So if the radio is checked bind a class (selected) to the div.

<div class="sau-select lg center" :class="selected">
   <label for="windows">
      <input type="radio" id="windows" value="windows" v-model="selectedOs" :checked="checked">
      <span></span>
      <img src="/img/windows.gif" alt="Windows">Windows
   </label>
</div>
like image 729
ottz0 Avatar asked Mar 16 '18 00:03

ottz0


People also ask

How do you checked radio button in VUE JS?

You can perform a condition against the checked property value of the radio button. Each of the radio buttons needs to be set a unique value. Let's say we have two radio buttons that will have a label text 'Yes' and 'No' with a supplied value of 1 represents the Yes value and 2 represents the No value.

How do you check checkbox is checked or not in VUE JS?

isCheckAll — It is a boolean type variable. This use to find whether checkall checkbox checked or not. languages — An Array type variable. It is used on the v-model directive of checkboxes.

What is @input in VUE JS?

It provides two-way data binding by binding the input text element and the value binded to a variable assigned.

What is V-model lazy?

lazy. By default, v-model syncs with the state of the Vue instance (data properties) on every input event - which means every single time the value of our input changes. The . lazy modifier changes our v-model so it only syncs after change events. The change event is triggered when a change is commited.


1 Answers

The :class="selected" you use hasn't much effect.

To conditionally apply a class to the div, you will have to use as :class condition if the selectedOs equals the value attribute of each <input>, such as :class="{redborder: selectedOs === 'windows'}". More details in the demo below:

new Vue({
  el: '#app',
  data: {
    selectedOs: 'windows'
  }
})
.redborder { border: 1px solid red; }
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <label :class="{redborder: selectedOs === 'windows'}">
    <input type="radio" value="windows" v-model="selectedOs"> Windows
  </label>

  <label :class="{redborder: selectedOs === 'linux'}">
    <input type="radio" value="linux" v-model="selectedOs"> Linux
  </label>
</div>

Notice that value="linux" means that that radio box will assign the string "linux" to the v-model variable (in this case selectedOs). value="linux" is equivalent to :value="'linux'", for instance.

like image 154
acdcjunior Avatar answered Sep 30 '22 14:09

acdcjunior