第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Vue 觀察兩個(gè)鏈接的輸入字段,僅供用戶輸入

Vue 觀察兩個(gè)鏈接的輸入字段,僅供用戶輸入

回首憶惘然 2023-02-24 10:44:14
我有兩個(gè)輸入字段,想法是兩者都可以改變另一個(gè)的值。如果在 level 字段中輸入 1 - 200 的值,Vue 會(huì)查找該級(jí)別并返回該級(jí)別的點(diǎn)數(shù)并將它們輸入到 points 字段中。但我也希望發(fā)生相反的情況,因此如果用戶在點(diǎn)字段中輸入一定數(shù)量的點(diǎn),Vue 會(huì)查找這些點(diǎn)并返回與該點(diǎn)數(shù)關(guān)聯(lián)的級(jí)別。這是可行的,除了 Vue 會(huì)進(jìn)行雙重計(jì)算,因?yàn)樗诒O(jiān)視兩個(gè)字段,所以如果我在點(diǎn)字段中輸入 300,它會(huì)正確返回級(jí)別 4,但 Vue 會(huì)檢測(cè)到級(jí)別字段中的變化,然后將點(diǎn)更改為 250。我只希望 Vue 檢測(cè)用戶何時(shí)更改字段,而不是在更改字段后重新計(jì)算。我希望這是有道理的,這很難解釋。從 axios 請(qǐng)求返回的數(shù)據(jù)示例:-[    {"level":1,"points":0},    {"level":2,"points":72},    {"level":3,"points":175},    {"level":4,"points":250},    {"level":5,"points":401,}]一個(gè)簡(jiǎn)化的 vue 組件來(lái)演示我的工作:-<template>    <div>        <div class="card-body">            <form>                <div class="row">                    <div class="col">                        <div class="form-group">                            <input type="text" v-model="currentLevel" class="form-control">                        </div>                        <div class="form-group">                            <input type="text" v-model="currentPoints" class="form-control">                        </div>                    </div>                </div>            </form>        </div>    </div>   </template><script>    export default {         data() {            return {                currentLevel: 1,                currentPoints: 0,                pointsTable: []            }        },        mounted() {            axios.get('/api/points').then(this.fetchPointsTable);        },        methods: {            fetchPointsTable({data}) {                this.pointsTable = data;            }        },        watch: {            currentLevel: function() {                 const result = this.pointsTable.find( ({ level }) => level === parseInt(this.currentLevel) );                this.currentPoints = result.experience;            },
查看完整描述

1 回答

?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

在這種情況下,您正在尋找change事件,該事件將在用戶更改輸入值時(shí)專門激活。

當(dāng)用戶提交對(duì)元素的更改時(shí),將為<input><select>和元素觸發(fā) change 事件。與事件不同,事件不一定會(huì)在元素的.<textarea>valueinputchangevalue

這意味著無(wú)論何時(shí)用戶更改任一輸入中的值,change事件都會(huì)觸發(fā),觸發(fā)關(guān)聯(lián)的方法并更新另一個(gè)框。這在沒有任何干擾的情況下發(fā)生,因?yàn)榱硪粋€(gè)輸入在其值以編程方式更新時(shí)不會(huì)觸發(fā)change事件(與跟蹤所有值更改的觀察者不同),因此不會(huì)觸發(fā)其關(guān)聯(lián)的方法。

你的例子的調(diào)整版本:

<template>

? ? <div>

? ? ? ? <div class="card-body">

? ? ? ? ? ? <form>

? ? ? ? ? ? ? ? <div class="row">

? ? ? ? ? ? ? ? ? ? <div class="col">

? ? ? ? ? ? ? ? ? ? ? ? <div class="form-group">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <input

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? type="text"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? v-model="currentLevel"?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? class="form-control"?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @change="updateCurrentPoints"

? ? ? ? ? ? ? ? ? ? ? ? ? ? >

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? ? ? <div class="form-group">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <input

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? type="text"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? v-model="currentPoints"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? class="form-control"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @change="updateCurrentLevel"

? ? ? ? ? ? ? ? ? ? ? ? ? ? >

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? </form>

? ? ? ? </div>

? ? </div>? ?

</template>


<script>

? ? export default {?

? ? ? ? data() {

? ? ? ? ? ? return {

? ? ? ? ? ? ? ? currentLevel: 1,

? ? ? ? ? ? ? ? currentPoints: 0,

? ? ? ? ? ? ? ? pointsTable: []

? ? ? ? ? ? }

? ? ? ? },

? ? ? ? mounted() {

? ? ? ? ? ? axios.get('/api/points').then(this.fetchPointsTable);

? ? ? ? },

? ? ? ? methods: {

? ? ? ? ? ? fetchPointsTable({data}) {

? ? ? ? ? ? ? ? this.pointsTable = data;

? ? ? ? ? ? },

? ? ? ? ? ? updateCurrentPoints() { // contents of currentLevel watcher

? ? ? ? ? ? ? ? const result = this.pointsTable.find( ({ level }) => level === parseInt(this.currentLevel) );


? ? ? ? ? ? ? ? this.currentPoints = result.experience;

? ? ? ? ? ? },

? ? ? ? ? ? updateCurrentLevel() { // contents of currentPoints watcher

? ? ? ? ? ? ? ? var levels = [];

? ? ? ? ? ? ? ? var goal = this.currentXp;


? ? ? ? ? ? ? ? var closest = this.xpTable.reduce(function(prev, curr) {

? ? ? ? ? ? ? ? ? ? if (curr.points <= goal) {

? ? ? ? ? ? ? ? ? ? ? ? levels = curr.level;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? return Math.max(levels);

? ? ? ? ? ? ? ? });


? ? ? ? ? ? ? ? this.currentLevel = closest;

? ? ? ? ? ? },

? ? ? ? }

? ? }

</script>


查看完整回答
反對(duì) 回復(fù) 2023-02-24
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)