简体   繁体   English

vue2 datepicker如何获取更改日期的输入元素的ID?

[英]vue2 datepicker How do I get the id of the input element where the date was changed?

I have a table with multiple rows and each row has a datepicker. 我有一个包含多行的表,每行都有一个日期选择器。 When a date is changed, how do I know which input element the date was changed for? 更改日期后,如何知道更改日期的输入元素?

@change event only gives me the new value that was selected. @change事件只会为我提供所选的新值。

try putting a $event in the arguments of your @change function: https://012.vuejs.org/guide/events.html 尝试将$ event放入@change函数的参数中: https ://012.vuejs.org/guide/events.html

it should show up if console the event in the change function, something like 如果在更改功能中控制台事件,它将显示

console.log(event.target.id)

let me know if that works! 让我知道是否可行!

Matt is correct but by default Vue automatically inserts the event as a parameter into the event listeners, so no need to pass in the $event unless you are expecting some extra parameters into the @change event. Matt是正确的,但默认情况下Vue会自动将事件作为参数插入事件侦听器,因此除非您期望在@change事件中添加一些参数,否则无需传递$event event。

 const app = new Vue({ el: '#app', template:'<table> <tr> <td><input type="text" id="1" @change="changed"></td> </tr> <tr> <td><input type="text" id="2" @change="changed" ></td> </tr> </table>', methods: { changed(event) { console.log('ID is:' + event.target.id); }, } }); 
 table, td { border: 1px solid black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"></div> 

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

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