javascript - VueJS : Getting Unique ID and Value of an array of input at the same time

标签 javascript html vue.js vuejs2 element-ui

我是 Vue 新手,仍在学习使用它。我正在学习使用 Element UI对于 Vue 用户界面。具体来说,我正在尝试 Input Number Component , 对于一组数据。假设我有这样的数据:

dataList = [{
    id: 1,
    productName: "ABC",
    qty: 1
}, {
    id: 2,
    productName: "DEF",
    qty: 2
}];

元素是这样的:

<div v-for="(item, index) in dataList" v-bind:key="item.id">
    <el-input-number v-model="item.qty" @change="handleChange"></el-input-number>    
</div>

脚本是这样的:

<script type="text/javascript">
    handleChange = function (value) {
        console.log(value); /* I need the item.id also not just the qty value */
    };
</script>

从函数 handleChange() 我只能得到输入数字的值,但不能得到我在元素中分配的项目 ID。我怎样才能同时获得这两个(item.id 和值(value))?

我期待像这样的 js 函数可以工作,但它没有:

handleChange = function(item, value) { /* */ }

我一直在尝试通过谷歌搜索一些答案,但答案总是只显示我可以从 change 事件中获取的 1 个参数。

如有任何帮助,我们将不胜感激,谢谢。

最佳答案

您可以将您的值 ($event) 作为第一个参数传递,将另一个参数作为第二个参数传递:

 <div v-for="(item, index) in dataList" v-bind:key="item.id">
   <el-input-number v-model="item.qty" @change="handleChange($event,item.id)"></el-input-number>    
</div>

脚本:

 <script type="text/javascript">
     handleChange = function (value,id) {
        console.log(value,id); 
     };
</script>

关于javascript - VueJS : Getting Unique ID and Value of an array of input at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553055/

相关文章:

node.js - 将 webpack-dev-server 更新到 3.2.1 后的 Vue.js 项目 - 引发错误

javascript - Vue.js Fetch 抛出 404 和 api 响应

javascript - 将标记模板文字函数的参数传递给另一个函数?

javascript - axios请求: promise error UnhandledPromiseRejectionWarning after catch anyway

javascript - 出现错误时看不到源代码

javascript - 每次生成动态行,同时关注最后一行元素

javascript - 我的 AJAX 调用不起作用

html - 为什么背景颜色不适用于 DIV 容器?

javascript - 带有路径连接器的 jQuery 可拖动小部件

javascript - 在 vue 组件上设置 props 值