在所生成的网址中将%+替换为%20 [英] Replace + with %20 in URL generated
问题描述
我使用下面的HTML代码将用户连接到聊天,但我需要生成的URL中的空格变为%20而不是+,因为+是昵称中的有效字符,所以输入<输入中的code> Paul John 会变成 Paul + John
,而不是在聊天中,因此需要%20作为一个空间。
是否可以通过JavaScript / jQuery和一些示例来实现这一点?
$ b
HTML
< form class =form-horizontalrole =formaction =client / htmlchat / 123flashchat.html> ;
< fieldset>
< input type =hiddenname =init_roomvalue =1>
<! - 文字输入 - >
< div class =form-group>
< label class =col-sm-2 control-labelfor =nickname>昵称< / label>
< div class =col-sm-10>
< input id =nicknamename =init_usertype =textplaceholder =Nicknameclass =form-controlrequired =required>
< / div>
< / div>
<! - 多个收音机 - >
< div class =form-group>
< label class =col-sm-2 control-labelfor =gender> Gender< / label>
< div class =col-sm-10>
< label class =radiofor =gender-0>
< input type =radioname =init_genderid =gender-0value =2required =required>
女性
< / label>
< label class =radiofor =gender-1>
< input type =radioname =init_genderid =gender-1value =1required =required>
男性
< / label>
< / div>
< / div>
<! - Button - >
< div class =form-group>
< div class =col-sm-offset-2 col-sm-10>
< / div>
< / div>
< / fieldset>
< / form>
在此先感谢,
Paul
encodeURIComponent(str)
函数。 b $ b
var test =Paul John;
var result = encodeURIComponent(test);
//结果:Paul%20John
在提交使用这个jquery代码:
$(document).ready(function(){
$('。form ('#nickname')。val()。trim());
$('#nickname')。val (编码);
});
});
I am using the below HTML code to connect a user to a chat, but I need any spaces in the URL generated to become %20 and not +, because + is a valid character in a nickname, so someone who enters Paul John
in the input will become Paul+John
instead in the chat, hence the %20 is needed as this is recognised as a space.
Is it possible to do this with JavaScript/jQuery and some example?
HTML
<form class="form-horizontal" role="form" action="client/htmlchat/123flashchat.html">
<fieldset>
<input type="hidden" name="init_room" value="1">
<!-- Text input-->
<div class="form-group">
<label class="col-sm-2 control-label" for="nickname">Nickname</label>
<div class="col-sm-10">
<input id="nickname" name="init_user" type="text" placeholder="Nickname" class="form-control" required="required">
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-sm-2 control-label" for="gender">Gender</label>
<div class="col-sm-10">
<label class="radio" for="gender-0">
<input type="radio" name="init_gender" id="gender-0" value="2" required="required">
Female
</label>
<label class="radio" for="gender-1">
<input type="radio" name="init_gender" id="gender-1" value="1" required="required">
Male
</label>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-sm-2 control-label" for="button"></label>
<div class="col-sm-offset-2 col-sm-10">
<button id="button" class="btn btn-lg btn-primary" type="submit">Enter</button>
</div>
</div>
</fieldset>
</form>
Thanks in advance,
Paul
Try using encodeURIComponent(str)
function.
var test = "Paul John";
var result = encodeURIComponent(test );
//result: "Paul%20John"
To replace the content just before submitting the form use this jquery code:
$(document).ready(function(){
$('.form-horizontal').submit(function(){
var encoded = encodeURIComponent($('#nickname').val().trim());
$('#nickname').val(encoded );
});
});
这篇关于在所生成的网址中将%+替换为%20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!