我如何在 javascript 中用 %20 替换空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12141251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i replace Space with %20 in javascript
提问by SDLBeginner
Possible Duplicate:
jQuery / Javascript replace <space> in anchor link with %20
I am getting sParameter like this :
我得到这样的 sParameter :
sParameter = document.getElementById('ddParameterType').value;
sParameter = document.getElementById('ddParameterType').value;
If i am getting word like "Test - Text"
as the ddParameterType item
then i am replacing the space in word like below:
如果我得到类似"Test - Text"
ddParameterType 项目的词,
那么我将替换词中的空格,如下所示:
sParameter = document.getElementById('ddParameterType').value.replace("","%20");
sParameter = document.getElementById('ddParameterType').value.replace("","%20");
but its returning a valur like %20Test - Text
.
I need like Test%20-%20Text
.
但它返回了一个像%20Test - Text
.
我需要像Test%20-%20Text
。
can anyone help.
任何人都可以帮忙。
回答by Esailija
回答by user1546010
replace replace("","%20");
with replace(/ /g,"%20");
替换replace("","%20");
为replace(/ /g,"%20");
回答by Raab
sParameter = encodeURIComponent(sParameter.trim())