简体   繁体   English

在Firefox中的Select2上有多个选择的输入字段缓存问题

[英]Input field cache issues with multiple selections on Select2 in Firefox

Currently attempting to implement Select2 in my rails app so a user can send messages to multiple users, I adapted the remote data example code on the site for my app as follows: 当前正在尝试在我的rails应用程序中实现Select2,以便一个用户可以向多个用户发送消息,我为我的应用程序修改了网站上的远程数据示例代码 ,如下所示:

  $("#shareCurrentInput").select2({
    placeholder: "Search for a user",
    width: "350px",
    multiple: true,
    minimumInputLength: 3,
    ajax: { 
      url: "search.json",
      dataType: 'json',
      data: function (term, page) {
        return {
            search: term
        };
     },
    results: function (data, page) { 
        return {results: data};
    }
   },

  formatResult: userFormatResult,
  formatSelection: userFormatSelection,  

  escapeMarkup: function (m) { return m; } 
});


 function userFormatResult(user) {
    var markup = "<table class='user-result'><tr>";
    if (user.uid !== undefined ) {
        markup += "<td class='user-image'><img src='http://graph.facebook.com/" + user.uid + "/picture?width=50&height=50'/></td>";
     }
    markup += "<td class='user-info'><div class='user-title'>" + user.name + "</div>";
    markup += "</td></tr></table>";
    return markup;
 }

 function userFormatSelection(user) {
    return user.name;
 }

This works as I intend it to in Safari, but in Firefox, I believe due to the fact that by default the browser caches input values from session to session, it causes issues when refreshing the page and attempting to select users. 这可以按照我在Safari中的预期工作,但在Firefox中,我相信由于默认情况下浏览器会在会话之间缓存输入值,因此刷新页面和尝试选择用户时会引起问题。 Is there a way to prevent Firefox from caching this in the code (ie without asking the user to turn off caching in their browser setting)? 有没有一种方法可以防止Firefox在代码中进行缓存(即不要求用户在其浏览器设置中关闭缓存)? Or is there another reason this code isn't working in Safari? 还是有其他原因导致此代码无法在Safari中运行?

I've tried using this code to disable autocomplete through jQuery, and there doesn't seem to be a CSS method to disable form autocomplete. 我尝试使用此代码通过jQuery禁用自动完成功能,而且似乎没有CSS方法可以禁用表单自动完成功能。 There also seem to be issues with disabling autocomplete in Chrome. 在Chrome中禁用自动填充功能似乎也存在问题 Is there an optimum cross-browser solution? 有没有最佳的跨浏览器解决方案? Autocomplete is off by default in the input field in Select2, and the input is not part of a form, so this solution does not seem possible. 默认情况下,Select2的输入字段中的“自动完成”处于关闭状态,并且输入不是表单的一部分,因此似乎无法实现此解决方案

EDIT: As per this page , the issue is only when I refresh the page, if I press enter in the address bar then there is no issue. 编辑:按照此页面 ,问题仅在刷新页面时出现,如果在地址栏中按Enter键,则没有问题。

Just added this code: 刚刚添加了以下代码:

$('#shareCurrentInput').val('');

Seems to work even on refresh in Firefox. 似乎甚至可以在Firefox中刷新。

If similar issue is happening with page refresh/reload using jQuery, try 如果使用jQuery刷新/重新加载页面时发生类似问题,请尝试

document.location.reload(true); document.location.reload(true);

instead of document.location.reload(); 而不是document.location.reload();

Details: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload 详细信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/Location/reload

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

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