简体   繁体   English

为 React 应用程序中的所有输入关闭自动完成

[英]Turn off autoComplete for all inputs in React app

This answer explains how to turn off autocomplete for an entire HTML document with jQuery .此答案解释了如何使用jQuery关闭整个 HTML 文档的autocomplete

Without jQuery is there a way to turn "off" React's equivalent autoComplete for all inputs within the app?如果没有 jQuery,有没有办法为应用程序中的所有输入“关闭”React 的等效autoComplete功能?

I'm not very familiar with jQuery otherwise I'd try to convert the function myself.我对 jQuery 不是很熟悉,否则我会尝试自己转换函数。

To disable the autocomplete for input fields in React, we need to set the input field's autoComplete attribute to "new-password".要在 React 中禁用输入字段的自动完成功能,我们需要将输入字段的 autoComplete 属性设置为“new-password”。

<input type="email" name="email_signup" placeholder="Email" autoComplete="new-password" />
<input type="password" name="password_signup" placeholder="Password" autoComplete="new-password" />

This will turn off autocomplete for input.这将关闭输入的自动完成功能。

You can use autocomplete="off" on the parent level of the form and that will turn it off for the entire form您可以在表单的父级使用autocomplete="off" ,这将关闭整个表单

<form method="post" action="/form" autocomplete="off">
[…]
</form>

在元素中使用它:

autoComplete:'off' 

The post that you are linking is just assigning the autocomplete off attribute to the document when it is loading.您正在链接的帖子只是在加载文档时将 autocomplete off 属性分配给文档。 You can accomplish what jQuery is doing with something like the following using vanilla JS.您可以使用 vanilla JS 完成 jQuery 正在执行的操作,如下所示。 However, I would suggest a more "React" way of doing things based on whatever logic you wish to accomplish.但是,我建议根据您希望完成的任何逻辑,采用更“反应”的做事方式。

var foo = document.querySelector("input"); 
foo.setAttribute("autoComplete", "off");

你可以用

<input name="myinput" placeholder="enter your name" value={this.input.myinput} autocomplete="off" />

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

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