javascript - 如何在按键上获取当前值
问题描述
我正在使用 vue.js 并在表内创建一个表我有一个输入字段,quantity
所以当我输入第一个单词时它正在empty
控制台上打印
假设我正在输入 3,然后显示为空,如果我正在输入 44,那么它会打印 4,我正在使用v-on:keypress
我不知道这里出了什么问题来捕获事件
var app = new Vue({
el: "#app",
data: {
invoice_products: [{
product_no: '',
product_name: '',
product_price: '',
product_qty: '',
line_total: 0
}]
},
methods: {
calculateLineTotal(invoice_product) {
console.log(invoice_product.product_qty)
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.6/vue.js"></script>
<div id="app">
<table>
<thead>
<tr>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr v-for="(invoice_product, k) in invoice_products" :key="k">
<td>
<input class="form-control text-right" type="number" min="0" step=".01" v-model="invoice_product.product_qty" v-on:keypress="calculateLineTotal(invoice_product)" />
</td>
</tr>
</tbody>
</table>
</div>
解决方案
You should use keyup
event handler.
KeyPress
, KeyUp
and KeyDown
are analogous to, respectively: Click
, MouseUp,
and MouseDown
.
Down
happens firstPress
happens second (when text is entered)Up
happens last (when text input is complete).
var app = new Vue({
el: "#app",
data: {
invoice_products: [{
product_no: '',
product_name: '',
product_price: '',
product_qty: '',
line_total: 0
}]
},
methods: {
calculateLineTotal(invoice_product) {
console.log(invoice_product.product_qty)
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.6/vue.js"></script>
<div id="app">
<table>
<thead>
<tr>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr v-for="(invoice_product, k) in invoice_products" :key="k">
<td>
<input class="form-control text-right" type="number" min="0" step=".01" v-model="invoice_product.product_qty" v-on:keyup="calculateLineTotal(invoice_product)" />
</td>
</tr>
</tbody>
</table>
</div>
推荐阅读
- amazon-web-services - 如何在 Amazon Forecast 相关时间序列中正确使用 in_stock
- sql - SAS YYMMDD10。有效,但 YYMMDDn10 无效
- regex - 带有记事本++的正则表达式:删除两组文本
- reactjs - Ionic React v5:配置项目以处理 Sass/Scss 文件
- ruby - 比较两个范围/数组时如何返回匹配项和非匹配项
- java - 读取文件的 Java 代码未在 VS 代码编辑器上编译
- php - 终端中的php openssl库未加载错误
- java - 如何使用对象类型作为方法/构造函数的参数?
- asp.net-core - ASP.NET Core 共享 Cookie 问题
- google-apps-script - 如何在 Gsheet 单元格上显示来自 Google Drive 的图像?