简体   繁体   English

vue.js 更改值未在输入 v-model 中更新

[英]vue.js change value not updated in input v-model

I'm trying to format number i put in input, i call a function to do this when i left the input but the value is not updated in the v-model.我正在尝试格式化输入输入的数字,当我离开输入但值未在 v-model 中更新时,我调用 function 来执行此操作。

The function works fine because i alert the value but it never updated in view. function 工作正常,因为我提醒了该值,但它从未在视图中更新。

Any idea?任何想法?

html html

    <div v-for="year in years">    
       <input type="text" :disabled="budget.checked[year] == true" v-on:blur="formatMoney(budget.personnelBudget[year])" v-model="budget.personnelBudget[year]"/>
       <input type="text"  :disabled="budget.checked[year] == true" v-on:blur="formatMoney(budget.travellingBudget[year])" v-model="budget.travellingBudget[year]" />
       <input type="text"  :disabled="budget.checked[year] == true" v-on:blur="formatMoney(budget.inventoriableBudget[year]" v-model="budget.inventoriableBudget[year]" />

    .....

js js

 data: function(){
            return{
                   budget:{
                    
                    personnelBudget:[],
                    travellingBudget:[],
                    inventoriableBudget:[],
                    consumablesBudget:[],
                    indirectExpensesPercent:[],
                    indirectExpensesBudget:[],
                    totalBudget:[],
                    checked:[],
                },
},

methods: {  
          
      formatMoney(input) {
                                            
                              
           this.budget.personnelBudget[year]=this.budget.personnelBudget[year]
                                              .replace(/,/g, "")
                                                    
           this.budget.personnelBudget[year]=parseFloat(this.budget.personnelBudget[year])
                                          .toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
           
           alert(this.budget.personnelBudget[year])
                                            
      },

You've a reactivity issue because you're assigning a value to a nested field which is not reflected in template, to solve this try to use this.$set :您有反应性问题,因为您正在为未反映在模板中的嵌套字段分配值,要解决此问题,请尝试使用this.$set

this.$set(this.budget,'personnelBudget',
 {...this.budget.personnelBudget, 
  [year]:this.budget.personnelBudget[year].replace(/,/g, "")})

then try try pass the input as string:然后尝试尝试将输入作为字符串传递:

   <input ... v-on:blur="formatMoney('personnelBudget',year)" v-model="budget.personnelBudget[year]"/>

and

 formatMoney(input,year) {
                           
  this.$set(this.budget,'personnelBudget',
    {...this.budget[input], 
     [year]:this.budget[input][year].replace(/,/g, "")})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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