event.target.value不返回预期值

发布于 2025-02-08 11:31:15 字数 648 浏览 4 评论 0原文

我的代码正常工作,但我很想知道为什么...我认为这与内存中的参考有关,但我找不到直接答案。

在某些情况下,我必须修改每个输入上电话字段的值,以修剪字符串之间的空格。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2025-02-15 11:31:15

这仅仅是因为您在第一个块中为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文