Home  >  Q&A  >  body text

How to set session value input in Vue.js

<p>I have a Vue.js form filter: </p> <pre class="brush:php;toolbar:false;"><b-row> <b-col md="3"> <b-form-group> <label>Name: </label> <b-form-input placeholder="name" type="text" class="d-inline-block" v-model="name" v-on:change="changeFilter()" /> </b-form-group> </b-col> <b-col md="3"> <label>Gender: </label> <v-select :options="genderOptions" class="w-100" v-model="gender" v-on:change="changeFilter()" /> </b-col> </b-row></pre> <p>I have a <code>methods</code> to get the input and selected values: </p> <pre class="brush:php;toolbar:false;">methods: { changeFilter() { console.log(this.name, this.gender) }, },</pre> <p>Now I want to reload the page but the input box or select box still shows the value I selected. please help me. Thank you</p>
P粉147045274P粉147045274420 days ago495

reply all(1)I'll reply

  • P粉885035114

    P粉8850351142023-08-26 20:14:46

    Okay, so the first good practice is not to use cangeFilter() on the click event, but to use changeFilter. So if I understand you correctly, you want to reload the page and still have the same value selected. You can save this value in local storage, assuming it's not sensitive data.

    Local storage information Save data in local storage

    localStorage.setItem('gender', 'Male');

    Get data (you need to use a life cycle hook like mounted to initialize the data)

    mounted() {
       this.gender = localStorage.getItem('gender') || '你的默认值';
    }
    
    
    <b-form-input
                placeholder="Name"
                type="text"
                class="d-inline-block"
                v-model="gender"
                v-on:change="changeFilter"
                :value="gender"
              />

    Values ​​in HTML input form

    reply
    0
  • Cancelreply