我有two text boxes,兩個文本框都有價值。
但我想得到value of one text box based on id number我將進入的。

如果我輸入 1 it should show value of text box 1.
這是我嘗試過的,現在它顯示了來自兩個文本框的資料。
模板:
<input v-model="textdata1" id="1" type="text" placeholder="i am id1, show my value">
<input v-model="textdata2" id="2" type="text" placeholder="i am id2, show my value">
<input v-model="searchid" type="text" placeholder="which ids data you want, 1 or 2 ">
<button @click="getvalue">RECEIVE</button>
<div>show value of id 1or2 here: {{id1data}}</div>
VUEJS:
<script>
import { defineComponent,ref } from 'vue'
export default defineComponent({
setup() {
const id1data =ref("");
const id2data =ref("");
function getvalue(){
this.id1data=this.textdata1;
this.id2data=this.textdata2;
}
return{
id1data,
id2data,
getvalue,
}
}
})
</script>
uj5u.com熱心網友回復:
如果您堅持使用 ID,這里有一種方法可以做到這一點:
<script>
import { defineComponent, ref } from "vue";
export default defineComponent({
setup() {
const value = ref("");
const searchid = ref("");
const searchidinput = ref("");
function getvalue() {
this.searchid = this.searchidinput
const el = document.getElementById(this.searchid);
if (el) {
this.value = el.value
}
}
return {
value,
searchid,
searchidinput,
getvalue,
};
},
});
</script>
<template>
<input id="1" type="text" placeholder="i am id1, show my value" />
<input id="2" type="text" placeholder="i am id2, show my value" />
<input v-model="searchidinput" type="text" placeholder="which ids data you want, 1 or 2 " />
<button @click="getvalue">RECEIVE</button>
<div>show value of id {{ searchid ? searchid : "<none>" }} here:
{{ value ? value : "none selected"}}
</div>
</template>
<style scoped>
a {
color: #42b983;
}
</style>
uj5u.com熱心網友回復:
這是一種方法。您可以為每個輸入創建一對資料值。
<script>
import { defineComponent, ref } from "vue";
export default defineComponent({
setup() {
const textdata1 = ref("");
const id1data = ref("");
const textdata2 = ref("");
const id2data = ref("");
const searchid = ref("");
const searchidinput = ref("");
function getvalue() {
this.searchid = this.searchidinput;
switch (this.searchid) {
case "1":
this.id1data = this.textdata1;
break;
case "2":
this.id2data = this.textdata2;
break;
}
}
return {
textdata1,
id1data,
textdata2,
id2data,
searchid,
searchidinput,
getvalue,
};
},
});
</script>
<template>
<input v-model="textdata1" id="1" type="text" placeholder="i am id1, show my value" />
<input v-model="textdata2" id="2" type="text" placeholder="i am id2, show my value" />
<input v-model="searchidinput" type="text" placeholder="which ids data you want, 1 or 2 " />
<button @click="getvalue">RECEIVE</button>
<div>show value of id {{ searchid ? searchid : "<none>" }} here:
{{ searchid === "1" ? id1data : searchid === "2" ? id2data : "none selected"}}
</div>
</template>
<style scoped>
a {
color: #42b983;
}
</style>
reactive()出于簡潔和組織目的,您也可以改用使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368239.html
標籤:javascript Vue.js Vuejs2 Vue 组件 Vuejs3
上一篇:如何為切換按鈕添加滑動影片?
下一篇:將prop物件克隆為新的資料物件
