简体   繁体   中英

HTML input element resets to default value

I have html input element with default value. I also have dropdown element. The problem is when I have some value typed in input and change/select the dropdown option, the input value resets to default one. How do I stop this from happening?

The problem is with v-model, if I remove it, it works as expected code snippet.

     <template>
                     <label for="name" class=""
                        >Name
                        <span class="">*</span>
                      </label>
                      <input
                        type="text"
                        class=""
                        placeholder="Name"
                        :value="defaultName"
                      />
     <select
                  v-model="myValue"
                  class=""
                  open-direction="bottom"
                  placeholder="Select" >
                  <option v-for="option in ['one', 'two', 'three']" v-bind:key="option">
                    {{ option }}
                  </option>
    </select>
        </template>
        <script>
        export default {
          data: function() {
            return {
        myValue: '',
        defaultName: 'My Name'
            }
        
        }
</script>

Looks like the problem is with combining v-model and :value

Try this code.

new Vue({
  el: "#app",
  data() {
    return {lights: true,
    test:["q","a"],
      myValue: '',
        defaultName: 'My Name',
    optionss:['one', 'two', 'three']
    };
  }
});


<div id="app">  
  <input
                        type="text"
                        class=""
                        placeholder="Name"
                        v-model="defaultName"
                      />
     <select
                  v-model="myValue"
                  class=""
                  open-direction="bottom"
                  placeholder="Select" >
                  </option> 
           <option v-for="options in optionss" :value="options">{{ options }}</option>

    </select>
  
  {{myValue}} {{defaultName}}
</div>

codepen - https://codepen.io/Pratik__007/pen/dyzZZjJ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM