jquery Select2 - 过滤特殊字符

标签 jquery jquery-select2

我正在使用 Selec2 来过滤表中的字段,它工作得很好。但 我在过滤掉带有特殊字符的单词时遇到问题,例如: “C” “A” “õ”

当我输入“navegaç”时,我的 Servlet 中会出现字符串“navegaç”,如下图所示。

enter image description here

enter image description here

select2 是否有任何设置可以更改字符集?

最佳答案

在 ajax 调用中,您需要使用 encodeURIComponent() 进行编码,然后再将其发送到服务器。

$('.js-example-basic-single').select2({
      ajax: {
        url : 'Controller',
        cache: false,
        data: function (params) {
          var query = {
            search      : encodeURIComponent(params.term),
            type        : 'public' 
          };
          return query;
        }
    }
});

在服务器中,您需要将此字符解码为 UTF-8。

String search = request.getParameter("search"); 
search = java.net.URLDecoder.decode(search , "UTF-8");

关于jquery Select2 - 过滤特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50399456/

相关文章:

javascript - 如何在内容加载后隐藏微调器

javascript - 强制 iframe 链接在同一 iframe 中打开

javascript - 使用ajax的select2加载数据无法选择任何选项

javascript - Select2 4.0.0 多值选择框-初始选择

Jquery Mobile 1.2RC-1 加载消息未显示

javascript - 尝试将多级对象传递给ajax

jQuery .autosuggest 保留结果

javascript - formatSelection 在 select2.js 中不起作用

javascript - 选择 2 : Getting formatSelection to have a certain context

javascript - 如何将 select2 设置为特定值?