event.target.value不返回预期值
我的代码正常工作,但我很想知道为什么...我认为这与内存中的参考有关,但我找不到直接答案。
在某些情况下,我必须修改每个输入上电话字段的值,以修剪字符串之间的空格。
const phoneInput = document.querySelector('.phoneInput')
phoneInput.addEventListener('input', function(event) {
const trimedValue = event.target.value.replace(/\D/g, '')
return event.target.value = trimedValue
})
这是完成并起作用的。
我的问题是,当我写这篇文章时,为什么不起作用?
const phoneInput = document.querySelector('.phoneInput')
phoneInput.addEventListener('input', function(event) {
return event.target.value.replace(/\D/g, '')
})
My code is working but I'm curious to know why... I think it has something to do with the reference in memory but I can't find a direct answer.
Some context, I had to modify the value of a phone field on each input to trim the spaces between the strings.
const phoneInput = document.querySelector('.phoneInput')
phoneInput.addEventListener('input', function(event) {
const trimedValue = event.target.value.replace(/\D/g, '')
return event.target.value = trimedValue
})
This is done and works.
My question is, why isn't it working when I write this ?
const phoneInput = document.querySelector('.phoneInput')
phoneInput.addEventListener('input', function(event) {
return event.target.value.replace(/\D/g, '')
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这仅仅是因为您在第一个块中为event.target.value分配了一些值,但是在第二个块中,您只是从event.target.value获得值并在其上执行一些操作。
It's simply because you're assigning some value to event.target.value in your first block but in your second block you're just getting the value from event.target.value and performing some operation on it.