当我尝试为此函数执行输出时,它将空格替换为+号。请有人帮帮我
图片包含我面临的问题:
function weightdifference(){
var weight;
var dreamweight;
var calDifference;
weight = parseFloat(document.bmiform.weight.value);
dreamweight = parseFloat(document.bmiform.dreamweight.value);
if (dreamweight > weight){
calDifference = (dreamweight - weight)/0.064;
return "You need" +calDifference+ "to your ideal weight";
}
else if (dreamweight < weight){
calDifference = (weight - dreamweight)/0.064;
return "You need" +calDifference+ "to your ideal weight";
}
}
function results(){
var calDifference = weightdifference();
var Difference = calDifference;
document.bmiform.Difference.value = Difference;
document.bmiform.submit();
}
结果页面
var difference;
difference = decodeURIComponent(getUrlVars()["Difference"]);
Days to Target Weight: <script>document.write(difference)</script><br>
谢谢!
答案 0 :(得分:0)
我猜你正在使用PHP服务器端并使用urlencode
对参数进行编码。 This answer就是为什么我猜这个。
如果您可以更改服务器端,请将其更改为使用rawurlencode
。
如果你不能,并且知道 +
将被编码(例如,%2b
),那么你可以安全地执行此操作:< / p>
difference = decodeURIComponent(getUrlVars()["Difference"].replace(/\+/g, "%20"));
在那里,我们将+
替换为空格的标准%20
序列,然后让decodeURIComponent
完成其工作。
但如果可以,请更新数据的编码方式。