我看过几十页的答案,但我找不到这个。
我有一个页面,可让人们创建任意数量的配置文件并为这些配置文件添加工具。他们可以给他们的个人资料任何名字,这就是问题所在。下面的解决方案有效,但它非常难看。这必须是更好的方法。
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),其中rawProfileName =“brita的个人资料”,但它提供了此错误消息。
Error: Syntax error, unrecognized expression: #profile_brita%2527s%20profile
throw new Error( "Syntax error, unrecognized expression: " + msg );
如果我使用ProfileName = $('')。text(profileName).html();