React中autoComplete="off" 失效

本文介绍了一种解决Chrome浏览器中React表单自动填充问题的方法。通过在表单顶部添加隐藏输入框并设置特定属性,可以有效防止浏览器自动填充表单字段。

Turning Off Autocomplete in Chrome with React

tl;dr Add a hidden input with an arbitrary value attribute at the top of the form, then set semantic values for the autocomplete property on the input fields for which you wish to disable autocomplete.

If you’re anything like me, you’ve just spent hours attempting to get Chrome to respect your autocomplete=”off” property on a form input. It’s one of those bizarre and annoying front-end quirks that can drive you to the brink of insanity. You’ve Googled the issue, tried the suggestions on every blog you’ve seen, and nothing works. Hopefully I have the answer for you.

I encountered this issue on a simple login form: one email input, and one password input. I tried setting autocomplete=”off” on the form level, on the input level, and on both levels. I tried putting these tags above the “real” ones:

None of that worked for me.

Eventually, I found this Chromium issue and tried the suggestion there, which was to put some arbitrary value in the autocomplete field. Unfortunately, when React rendered the HTML, it seemed that any value I put in the autoComplete property, was being transformed to “off”.

Like Aaron Rodgers in the 4th quarter, I threw up a Hail Mary, and tried sticking this in at the top of my form:

Success!

I refreshed a few times, just to make sure, and to my relief, the autofill nightmare was over… until I changed the autoComplete values in my JSX back to “off”.

As I mentioned before, when I tried using semantic values as suggested by the Chromium developers, the HTML rendered by React seemed to ignore them, and stick in “off” no matter what input I gave it.

 

 

Well, on some level, that’s not entirely true. I don’t really understand what’s going on yet, but I know this:

If I set autoComplete=”off” in my JSX, Chrome will autofill the fields.

If I set semantic values for autoComplete AND include this tag at the top of my form…

…Chrome will not autofill the fields.

Chrome is obviously reading the autocomplete values passed into it, then changing them for some reason.

Ahh, the joys of front-end development.

 

文章来自:https://medium.com/paul-jaworski/turning-off-autocomplete-in-chrome-ee3ff8ef0908

 

转载于:https://www.cnblogs.com/boyuguoblog/p/9173982.html

网页上有个框点击后会在下面出现很多可以选择的,但是我每次出现就只想选择到第一个的,别的都不要,他的css要怎么选择。下面是一些的html结构<div class="ait-select-selector"><span class="ait-select-selection-search"><input type="search" id="731" autocomplete="off" class="ait-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="731_list" aria-autocomplete="list" aria-controls="731_list" aria-activedescendant="731_list_0" aria-required="true" value=""></span><span class="ait-select-selection-placeholder">请选择</span></div>下面是这个框选择的好的<div class="ait-form-item ait-form-item-has-success"><div class="ait-row ait-form-item-row"><div class="ait-col ait-form-item-label"><label for="731" class="ait-form-item-required" title="">品名(Product name)<span class="edit-btn">修改</span></label></div><div class="ait-col ait-form-item-control"><div class="ait-form-item-control-input"><div class="ait-form-item-control-input-content"><div class="ait-select ait-select-in-form-item ait-select-status-success ait-select-single ait-select-show-arrow ait-select-show-search" aria-required="true"><div class="ait-select-selector"><span class="ait-select-selection-search"><input type="search" id="731" autocomplete="off" class="ait-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="731_list" aria-autocomplete="list" aria-controls="731_list" aria-activedescendant="731_list_1" aria-required="true" value="" aria-expanded="false" data-spm-anchor-id="a2g3i.%2Fproduct-publish-v2%2Fpop.0.i13.69fe23f1sh5zan"></span><span class="ait-select-selection-item"><div title="餐厅套装(restaurant sets)" data-spm-anchor-id="a2g3i.%2Fproduct-publish-v2%2Fpop.0.i15.69fe23f1sh5zan">餐厅套装(restaurant sets)</div></span></div><span class="ait-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" class="anticon ait-select-suffix aiticon aiticon-chevron-down"><svg viewBox="64 64 896 896" version="1.1" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><path d="M739.712 355.712a40 40 0 0 1 57.024 56.128l-0.448 0.448-256 256a40 40 0 0 1-56.128 0.448l-0.448-0.448-256-256a40 40 0 0 1 56.128-57.024l0.448 0.448L512 583.424l227.712-227.712z"></path></svg></span></span></div></div></div></div></div></div>下面的是没有选择的第二下拉框<div class="ait-form-item-control-input-content"><div class="ait-select ait-select-in-form-item ait-select-single ait-select-show-arrow ait-select-show-search" aria-required="true"><div class="ait-select-selector"><span class="ait-select-selection-search"><input type="search" id="732" autocomplete="off" class="ait-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="732_list" aria-autocomplete="list" aria-controls="732_list" aria-activedescendant="732_list_0" aria-required="true" value="" aria-expanded="false" data-spm-anchor-id="a2g3i.%2Fproduct-publish-v2%2Fpop.0.i61.69fe23f1sh5zan"></span><span class="ait-select-selection-placeholder">请选择</span></div><span class="ait-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" class="anticon ait-select-suffix aiticon aiticon-chevron-down"><svg viewBox="64 64 896 896" version="1.1" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><path d="M739.712 355.712a40 40 0 0 1 57.024 56.128l-0.448 0.448-256 256a40 40 0 0 1-56.128 0.448l-0.448-0.448-256-256a40 40 0 0 1 56.128-57.024l0.448 0.448L512 583.424l227.712-227.712z"></path></svg></span></span></div></div>下面是选择好的第二下拉框<div class="ait-col ait-form-item-control"><div class="ait-form-item-control-input"><div class="ait-form-item-control-input-content"><div class="ait-select ait-select-in-form-item ait-select-status-success ait-select-single ait-select-show-arrow ait-select-show-search" aria-required="true"><div class="ait-select-selector"><span class="ait-select-selection-search"><input type="search" id="732" autocomplete="off" class="ait-select-selection-search-input" role="combobox" aria-haspopup="listbox" aria-owns="732_list" aria-autocomplete="list" aria-controls="732_list" aria-activedescendant="732_list_0" aria-required="true" value="" aria-expanded="false" data-spm-anchor-id="a2g3i.%2Fproduct-publish-v2%2Fpop.0.i61.69fe23f1sh5zan"></span><span class="ait-select-selection-item"><div title="金绿柱石(heliodor)">金绿柱石(heliodor)</div></span></div><span class="ait-select-arrow" unselectable="on" aria-hidden="true" style="user-select: none;"><span role="img" class="anticon ait-select-suffix aiticon aiticon-chevron-down"><svg viewBox="64 64 896 896" version="1.1" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false" class=""><path d="M739.712 355.712a40 40 0 0 1 57.024 56.128l-0.448 0.448-256 256a40 40 0 0 1-56.128 0.448l-0.448-0.448-256-256a40 40 0 0 1 56.128-57.024l0.448 0.448L512 583.424l227.712-227.712z"></path></svg></span></span></div></div></div></div>
最新发布
08-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值