3

This may be trivial but I could not make a select box in Vue.js template to show US as default (that is before the drop down being clicked)

<select v-model="country">                                           
    <option selected="selected">US</option>
    <option>UK</option>
    <option>EU</option>
</select>

The problem is that selector appears blank before being clicked whatever I try.

How can I fix it?

7
  • 2
    What is wrong? What is not going well? What is the problem? Commented Sep 7, 2018 at 4:42
  • The selector appears blank before being clicked. Commented Sep 7, 2018 at 4:44
  • I made a file that has the same structure as yours, and it appears selected for me. Commented Sep 7, 2018 at 4:49
  • Please notice, I'm taliking about vue.js template not ordinary html. Commented Sep 7, 2018 at 4:50
  • OK, will test with Vue. What's the URL of the file you're using? Commented Sep 7, 2018 at 4:50

1 Answer 1

5

See Form Input Bindings - Select. You just need to set your bound v-model property to the appropriate value.

Here's an example...

new Vue({
  el: '#app',
  data: {
    country: 'US'
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<div id="app">
  <select v-model="country">
    <!-- as recommended by Vue -->
    <option disabled value="">Please select one</option>
    <option>US</option>
    <option>UK</option>
    <option>EU</option>
  </select>
  
  <pre>Selected country: {{ country }}</pre>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

I had tried that. Still US does not appear on the selector bar.
@Babr can you add your JavaScript code to your question? How and where do you assign a value to country?
Sorry Phil, stupid me! I had put country outside data! Thanks for the tip.

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.