admin管理员组文章数量:1400360
In a Vue app I have a paste listener
on a textarea
with the intention to run validation code when the user pastes data into this field. When I log the paste event I can see in the console that the data that was pasted into the field is there under event -> target -> value
. I can't seem to access this with event.target.value
though. What am I doing wrong?
Minimal example:
<div id="app">
<textarea name="myField" @paste="onPaste"></textarea>
<p>Field name: {{ fieldName }}</p>
<p>Pasted data: {{ pasted }}</p>
</div>
var app = new Vue({
el: '#app',
data: {
fieldName: '',
pasted: ''
},
methods: {
onPaste(event){
console.log(event)
this.message = event.target.name
this.paste = event.target.value
}
}
})
/
In a Vue app I have a paste listener
on a textarea
with the intention to run validation code when the user pastes data into this field. When I log the paste event I can see in the console that the data that was pasted into the field is there under event -> target -> value
. I can't seem to access this with event.target.value
though. What am I doing wrong?
Minimal example:
<div id="app">
<textarea name="myField" @paste="onPaste"></textarea>
<p>Field name: {{ fieldName }}</p>
<p>Pasted data: {{ pasted }}</p>
</div>
var app = new Vue({
el: '#app',
data: {
fieldName: '',
pasted: ''
},
methods: {
onPaste(event){
console.log(event)
this.message = event.target.name
this.paste = event.target.value
}
}
})
https://jsfiddle/feg8pcmv/
Share Improve this question edited Apr 11, 2020 at 20:47 Aymen TAGHLISSIA 1,93517 silver badges31 bronze badges asked Apr 11, 2020 at 18:16 jonas87jonas87 6743 gold badges8 silver badges25 bronze badges 02 Answers
Reset to default 3First of all, your jsfiddle has a minor typo (this.paste
instead of this.pasted
).
Secondly, you need to use the "getData" method from the clipboardData property to access the text.
https://developer.mozilla/en-US/docs/Web/API/Element/paste_event
this.pasted = event.clipboardData.getData('text')
https://jsfiddle/zfuwy9ep/
That said, if you want to get the whole string inside the text area after something was pasted, you could wait until the current items in the event queue have been resolved, and read the value of the textarea afterwards
setTimeout(() => {
console.log('textarea contents', event.target.value);
})
https://jsfiddle/cjq1bw5u/
Currently (as of now) according to MDN docs regarding onpaste event
The paste event fires when the user attempts to paste text.
Note that there is currently no DOM-only way to obtain the text being pasted; you'll have to use an nsIClipboard to get that information.
And since Clipboard API/events are in draft, supporting older browsers may not be achieved.
One not so neat solution is to use setTimeout
:
methods: {
onPaste(event){
setTimeout(() => {
console.log(event);
this.fieldName = event.target.name;
this.pasted = event.target.value }, 100);
}
}
More info can be found here
本文标签: javascriptAccessing paste event data with VueStack Overflow
版权声明:本文标题:javascript - Accessing paste event data with Vue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744220890a2595859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论