繁体   English   中英

Javascript字符串/无空间的变量串联

[英]Javascript string/variable concatenation without space

在使用ajax脚本时,我使php部分回显了一个值,然后在jQuery部分中提醒了它。

所以我们有这样的事情:

<?php echo "1"; ?>

//Javascript
alert("the value is"+phpvalue);

警报显示为“该值为1”。 现在,我的问题是如何删除“ value is”和+ phpvalue之间的空格? 因此它显示为“值是1”。 我在php部分试过trim(),但没有任何改变。

感谢任何帮助,如果这是一个菜鸟问题,对不起,谢谢//

在http中,响应可能是您打印的其他字符。 例如,如果php标记的开头是php标记.. BOM字符之前的空格...因此,如果仅修剪变量,则不会确定,您可以在客户端获得纯变量而没有垃圾字符。

因此,您应该在javascript中的客户端上使用trim:例如jQuery trim( http://api.jquery.com/jQuery.trim/

   alert("the value is"+$.trim(phpvalue));

或使用phpjsorg( http://phpjs.org/functions/trim/ )中的nativ js trim函数:

   alert("the value is"+trim(phpvalue));

不确定您到底想要什么,但有两种情况

If you just are dealing with excess whitespace on the beginning or end of the string you can use trim(), ltrim() orrtrim() to remove that.

If you are dealing with extra spaces within a string consider a preg_replace of multiple whitespaces " "* with a single whitespace " "

$foo = preg_replace( '/\s+/', ' ', $foo );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM