简体   繁体   English

切换自动完成打开/关闭表单字段

[英]toggling autocomplete on/off form field

I have a form field wherein a user enters their address. 我有一个表单字段,用户在其中输入他们的地址。 To make he user's life easier, I have added google places API address autocomplete to this field. 为了使用户的生活更加轻松,我在此字段中添加了Google place API地址自动填充功能。 Everything works great with this approach until you get in to using native browser autocomplete. 在您开始使用本机浏览器自动完成功能之前,一切都可以使用此方法很好地工作。 If a user has autocomplete set up on their web browser, I obviously want them to be able to use that... the faster through the form the better. 如果用户在其Web浏览器上设置了自动完成功能,显然我希望他们能够使用该功能…表单越快越好。 However, when a user is on my address field, the native autocomplete & the google places autocomplete end up competing with each other (native above the google places). 但是,当用户在我的地址字段中时,本机自动完成功能和Google地方信息自动完成功能最终会彼此竞争(在Google地方信息之上)。 It looks terrible, and is also a bad user experience for anyone who wanted to utilize the GP autocomplete. 它看起来很糟糕,对于想要使用GP自动完成功能的用户来说,也是糟糕的用户体验。

My natural inclination was to simply toggle autocomplete="off" on the address field (using focus and blur ): 我的自然倾向是简单地在地址字段上切换autocomplete="off" (使用focusblur ):

 $search_address
   //don't allow autocomplete when users are in this field as it messes with GP input
   .on('focus', function(e){
    $(this).attr('autocomplete', 'off'); console.log('autocomplete off');
   })

   //if the user is not in the field, allow autocomplete, or it messes with native AutoFill
   .on('blur', function(e){
    $(this).removeAttr('autocomplete'); console.log('autocomplete on');
   });

I can tell from my console logs that these are firing, however the native browser autocomplete is not being removed. 我可以从控制台日志中得知这些日志正在触发,但是未删除本机浏览器的自动完成功能。 I am not sure how to handle this situation. 我不确定如何处理这种情况。 Anyone think of a way to do this or possibly know why this doesn't work? 任何人都想办法做到这一点,或者可能知道为什么这行不通?

Not sure if it matters or not but you may want to try using prop instead of attr: 不知道这是否重要,但您可能想尝试使用prop而不是attr:

$(this).prop("autocomplete","off"); $(本).prop( “自动完成”, “关闭”);

I did and it worked for me in Chrome. 我做到了,它在Chrome中对我有用。

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

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