简体   繁体   English

使用 chrome 自动完成功能时,ruby on rails 应用程序出现故障

[英]ruby on rails app glitching when chrome auto complete is being used

I have a weird bug that hangs up or makes my app unusable or freezes for a minute.我有一个奇怪的错误,它挂起或使我的应用程序无法使用或冻结一分钟。

So I have a 2 forms side by side one for billing and one for shipping and it has a drop down that uses the select2 library.所以我有两个并排的表单,一个用于计费,一个用于运输,它有一个使用select2库的下拉列表。 When a user selects this drop down the chrome autocomplete shows up to.当用户选择此下拉菜单时,chrome 自动完成显示最多。 Its overlapping the select2 dropdown.它与 select2 下拉菜单重叠。 here's an image这是一张图片在此处输入图片说明

so the problem here is when the user uses that drop down to select a country and uses the chrome autofill instead of the select2 drop down and hits the enter key the form on the right fills the country and hangs up or freezes the app.所以这里的问题是当用户使用该下拉菜单选择一个国家并使用 chrome 自动填充而不是 select2 下拉菜单并按下回车键时,右侧的表格会填充该国家并挂断或冻结应用程序。 also when they choose a country or a state it will do a get request to the server.此外,当他们选择一个国家或州时,它会向服务器发出 get 请求。

What I tried are:我试过的是:

1) turn off the chrome autocomplete on the settings. 1)关闭设置中的chrome自动完成功能。 it works no issues.它没有问题。 the form on the right won't be updated (so I concluded it was chrome)右边的表格不会更新(所以我断定它是 chrome)

2) I added an html: {autocomplete: 'off'} but this doesn't work I don't know why. 2) 我添加了一个html: {autocomplete: 'off'}但这不起作用我不知道为什么。

here's the form:这是表格:

<%= form_for [:admin, @corporate_account], html: { autocomplete: "off" } do |f| %>
    <fieldset data-hook="new_property">

      <div class="row">
        <div class="col-md-12">
          <%= render partial: 'form', locals: { f: f } %>
        </div>
      </div>
      </div>
        <div class="col-md-12">
          <%= render partial: 'address_form', locals: { f: f } %>
        </div>
      </div>

      <div class="form-actions">
        <%= render partial: 'spree/admin/shared/new_resource_links' %>
      </div>
    </fieldset>
<% end %>

UPDATE更新

I found that when I hit the enter with the chrome autocomplete it sends a GET request.我发现当我用 chrome 自动完成输入时,它会发送一个 GET 请求。 but with the regular drop down from the app it doesn't, I think chromes autocomplete after hitting the enter key it sends the form.但是从应用程序的常规下拉菜单中它没有,我认为在点击它发送表单的回车键后 chromes 会自动完成。 hhmmmmmmmmm嗯嗯嗯嗯

We are using select2 for our searches.我们正在使用 select2 进行搜索。 It uses jQuery and ids are dynamically added.它使用 jQuery,并且 id 是动态添加的。 I found the script attaches the ID dynamically.我发现脚本动态附加了 ID。 It wasn't in the .js file, it was inside the .erb file.它不在.js文件中,而是在.erb文件中。 I've added this .js script inside the .erb file.我在.erb文件中添加了这个.js脚本。

<% content_for :head do %>
  <%= javascript_tag do %>
    $(document).ready(function(){
      $('span#<%= s_or_b %>country .select2').on('change', function() { update_state('<%= s_or_b %>'); });

      <%# bellow this comment the code that I added %>
      <%# this IDs are gnerated dynamically by select2 %>
      const countryID = $('span#<%= s_or_b %>country .select2 .select2-search input').attr('id')
      const stateID = $('span#<%= s_or_b %>state .select2 .select2-search input').attr('id')

      <%# add the type attribute search so autocomplete can be toggled on and off %>
      $(`#${countryID},#${stateID}`).attr('type', 'search')

      <%# on focus set the autocomplete attribute off %>
      $(`#${countryID},#${stateID}`).on('focus', function(){ 
        $(`#${countryID},#${stateID}`).attr('autocomplete', 'off')
       }) 

    });
  <% end %>
<% end %>

So when I focus on the search input, Chrome's autocomplete is not working any more.因此,当我专注于搜索输入时,Chrome 的自动完成功能不再起作用。

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

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