为什么在我的方法中,仅在将第二位数字放入输入值之后才计算方法

时间:2019-06-07 02:08:20

标签: vue.js methods

错误:-指定的值“ NaN”不是有效数字。该值必须与以下正则表达式匹配:-?(\ d + | \ d +。\ d + |。\ d +)([eE] [-+]?\ d +)?

当我将值放入数量和比率的输入值中时,输入喜欢的预期结果应为4 * 5 = 20,但不会这样做,而是4 * 5a,其中a是任何值的变量放置整数,则仅给出预期结果20

<table>
 <tr v-for="(pro,index,k) in pr`enter code here`oduct" :key="k">
<td>{{index+1}}</td>
<td><input type="number" v-model="pro.qnt" @keypress="calculatetotal(index)" style="width:110px;"></td>
<td><input type="number" v-model="pro.rate" @keypress="calculatetotal(index)" style="width:110px;"></td>
<input type="number" v-model="pro.totalamount" class="form-control"></td>
 <td><input type="number" v-model="pro.Amount" style="width:110px;" @keypress="discountamount(index)"></td>
                                    <td><input type="number" v-model="pro.discount" style="width:110px;"  @keypress="discountamount(index)" :disabled = "selected===1"></td>
                                    <td><input type="number" v-model="pro.totalamount" class="form-control"></td>
  </table>


methods:{
  calculatetotal: function(index)
            {
                var total;
                total=this.product[index].qnt * this.product[index].rate;
                this.product[index].Amount=total
            },

 discountamount: function(index)
            {

                //check whether the discoun is disabled or not
                if( this.selected===1)
                {
                  this.product[index].totalamount=this.product[index].Amount
                }
                else{
                var dsa;
                dsa=this.product[index].Amount - this.product[index].discount;
                this.product[index].totalamount = dsa
                }
            }

  }

输出应为2 * 4 = 8,但它给出2 * 4 = 0,并且当2 * 4a(其中a是整数可变)时给出正确答案,那么只能给出预期结果8

1 个答案:

答案 0 :(得分:0)

如果您查看此答案:https://stackoverflow.com/a/3396805/5543999 您会看到按下事件时,输入尚未更改。 将您的@keypress更改为@keyup,一切都会正常运行。