我看了几十页的答案,但我找不到这个。
我有一个页面,可以让人们创建任意数量的配置文件并向这些配置文件添加工具。他们可以给他们的个人资料起任何名字,这就是问题所在。下面的解决方案有效,但非常难看。这里必须有更好的方法来做到这一点。
var getProfile = jQuery.post('${pageContext.request.contextPath}/JSON?action=preferredTools&profileName='+cleanProfile, function(tool){},"json");
getProfile.done(function(tool){
uncheckAll(); // this is to remove any tools that may already be showing from the previous profile
document.getElementById("formProfileName").value=rawProfileName;
var profileName = rawProfileName.replace("\\", "\\\\").replace(/~/g,"\\~").replace(/!/g,"\\!").replace(/@/g, "\\@")
.replace(/\#/g, "\\#").replace(/\\$/g,"\\$").replace(/%/g,"\\%").replace(/\^/g,"\\^")
.replace(/\&/g,"\\&").replace(/\*/g,"\\*").replace(/\(/g,"\\(").replace(/\)/g,"\\)").replace(/_/g,"\\_")
.replace(/-/g,"\\-").replace(/\+/g,"\\+").replace(/\=/g,"\\=").replace(/\[/g,"\\[").replace(/\]/g,"\\]")
.replace(/\{/g,"\\{").replace(/\}/g,"\\}").replace(/\|/g,"\\|").replace(/\;/g,"\\;").replace(/\:/g,"\\:")
.replace(/\'/g,"\\'").replace(/\,/g,"\\,").replace(/\./g,"\\.").replace(/\?/g,"\\?")
.replace(/\</g,"\\<").replace(/\>/g,"\\>").replace(/\`/g,"\\`").replace(/\/ /g,"\\/");
$('#profile_'+profileName).prop("checked", true);
我尝试使用 escape(rawProfileName) where rawProfileName = "brita's profile" 但它给出了这个错误消息。
Error: Syntax error, unrecognized expression: #profile_brita%2527s%20profile
throw new Error( "Syntax error, unrecognized expression: " + msg );
如果我使用 ProfileName = $('').text(profileName).html();