我正在使用 Selec2 来过滤表中的字段,它工作得很好。但 我在过滤掉带有特殊字符的单词时遇到问题,例如: “C” “A” “õ”
当我输入“navegaç”时,我的 Servlet 中会出现字符串“navegaç”,如下图所示。
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/