简体   繁体   English

Event.target.value 未返回预期值

[英]Event.target.value not returning the expected value

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, '')
})

这仅仅是因为您在第一个块中为 event.target.value 分配了一些值,但在第二个块中,您只是从 event.target.value 获取值并对其执行一些操作。

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

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