javascript - 使用自动完成的值填充状态( react )

标签 javascript reactjs google-chrome autocomplete

我有一个带有输入字段的组件和一个禁用按钮,除非在输入字段中输入了正确的值。这工作正常,但是当 chrome 在页面加载时自动完成这些值时,状态不会改变,因此按钮保持禁用状态。如果您在表单内部单击或更改值,状态会正确更改。

如何在组件加载时从自动完成中获取值?自动完成不会触发 onChange 或 onBlur 事件,autocomplete="off"也不起作用。 (我也不想关闭自动完成)

最佳答案

我最近在处理登录页面时遇到了这个问题。经过一番研究,我遇到了 this issue在 ReactJS github 上报告。引用关键部分:

If we are able to access autofilled values once the page loads, it would become very easy to write malicious code to extract information on a form intended for phishing. Giving the user no chance to react. Probably part of the reason why autocomplete="off" is ignored by browsers.

To overcome this problem we need to:

  1. Accept the fact that we won't (and never will) have access to autofilled values when the DOM node mounts
  2. Embrace the security concerns which are brought up by the browsers

tl;dr - 出于安全原因,这是有意为之的,最好在提交时验证输入。

关于javascript - 使用自动完成的值填充状态( react ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51193481/

相关文章:

javascript - 如何将脚本标记插入到 div?

google-chrome - E : Repository 'http://dl.google.com/linux/chrome/deb stable Release' changed its 'Origin' value from 'Google, Inc.' to 'Google LLC'

javascript - 在“设置”屏幕中缺少 TabBar 查看图标

javascript - 如何在组件内添加相同的组件

javascript - jQuery if-else 工作 50%

python - 只有一个 python 程序在运行(比如 Firefox)?

css - PHP/HTML/CSS - 如果是 FireFox,是 Chrome 还是 Safari

javascript - 为什么我得到了错误的部门答案?

javascript - 即使更改超时后,请求超时也不会更改(Windows 操作系统)

javascript - 使用单选按钮、highcharts、vb.net 从服务器获取数据