Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs display other input fields based on selected value from dropdown

Tags:

jquery

vue.js

I have a select dropdown list and two hidden input fields. I would like when a user select first item in dropdown list , the first input field gets displayed and vice versa. Here is my code but I'm not sure how to do an if statement so when selected value equals xxx display input field 1, else display input field 2

new Vue({
      el: '#app',
      data: {
               selected: ''
             }
       });
<select name="parent" class="form-control" v-model="selected" required>
      <option value="" selected></option>
      <option value="item1">Item 1</option>
      <option value="item2">Item 2</option>
</select>
<div>
   <input name="Test 1" v-show="selected"> //display when item 1 is selected
</div>
<div>
   <input name="Test 2" v-show="selected"> //display when item 2 is selected
</div>

Thank you

like image 536
rafiaTech Avatar asked Dec 14 '22 09:12

rafiaTech


1 Answers

You can do something like

<div v-if="selected === 'item1'">
  item1 was selected
</div>
<div v-else>
  Something else was selected.
</div>

If you don't want to use v-else you can do:

<div v-if="selected != 'item1'">
  Something besides item1 was selected
</div>
like image 145
Bill Criswell Avatar answered Dec 18 '22 07:12

Bill Criswell