簡體   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