使用空格传递字符串变量 [英] Passing string variable with spaces
本文介绍了使用空格传递字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下代码中:
<script type="text/javascript">
function updateView(set) {
$.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
$( "#content" ).html( data );
});
}
</script>
'set'是一个字符串变量,其中可以包含空格。我注意到它有空格时它无法正常工作。我该如何解决这个问题?
'set' is a string variable which can have spaces in it. I'm noticing when it has spaces it's not working correctly. How can I fix this?
编辑:为了清楚起见,我想保持空格不变。
For clarity, I'd like to keep the spaces intact.
推荐答案
您必须用'%20'$ c $替换中间空间(
''
) c>使用 replace()
并使用 trim()消除边界空间(
。''
)
You have to replace intermediate space(' '
) with '%20'
with replace()
and eliminate boundary spaces(' '
) with trim()
.
所以请使用以下代码 -
So use the following code-
<script type="text/javascript">
function updateView(set) {
set=set.trim().replace(/ /g, '%20');
$.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
$( "#content" ).html( data );
});
}
</script>
这篇关于使用空格传递字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文