使用空格传递字符串变量
问题描述:
在以下代码中:
<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>