亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何與Vue交換輸入字段?

如何與Vue交換輸入字段?

智慧大石 2023-11-12 22:10:30
click當事件觸發時,我很難交換字段。下面是我的代碼,它只是更改了一次值和名稱,但如果再次單擊它就不起作用。它也不會改變該v-model值。有人可以幫我嗎?超文本標記語言<div id='app'>        <button @click="swapInputFields">Swap</button>  <input type="text" :name="[field1]" v-model="model1" :value="[fromCity]"><br>  <input type="text" :name="[field2]" v-model="model2" :value="[toCity]"></div>代碼查看:var app = new Vue({   el: '#app',   data: {      fromCity:'FROM',      toCity :'TO',      field1 :'name1',      field2 :'name2'   },   methods: {      swapInputFields()      {         this.fromCity = 'TO';         this.toCity ='FROM';         this.field1 = 'name2';         this.field2 ='name1';      }   }});
查看完整描述

1 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

從綁定中刪除括號,并從輸入中刪除除和[ ]之外的所有屬性:typev-model


<button @click="swapInputFields">Swap</button>

<input type="text" v-model="fromCity"><br>

<input type="text" v-model="toCity">

您只需要這些模型變量fromCity和toCity數據:


data() {

   return {

      fromCity:'FROM',

      toCity :'TO'

   }

},

并像這樣交換它們:


swapInputFields()

{

   const temp = this.fromCity;

   this.fromCity = this.toCity;

   this.toCity = temp;

}

這是一個演示:

new Vue({

  el: "#app",

  data(){

     return {

        fromCity:'FROM',

        toCity :'TO'

     }

  },

  methods: {

    swapInputFields()

    {

      const temp = this.fromCity;

      this.fromCity = this.toCity;

      this.toCity = temp;

    }

  }

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">

  <button @click="swapInputFields">Swap</button>

  <input type="text" v-model="fromCity"><br>

  <input type="text" v-model="toCity">

</div>


查看完整回答
反對 回復 2023-11-12
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號