使用空格传递字符串变量

编程入门 行业动态 更新时间:2024-10-19 20:41:16
本文介绍了使用空格传递字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在以下代码中:

<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'使用 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>

更多推荐

使用空格传递字符串变量

本文发布于:2023-11-22 06:23:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   字符串   变量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!