2014-09-22 83 views
-1

什麼是最好的方式來回顯變量get_parestotal?echo a javascript變量

請幫忙! 謝謝

<script type="text/javascript"> 
$(document).ready(function(){ 
        var get_parestotal = 0 
       $(".parestotal").each(function(){ 
        get_parestotal += parseFloat($(this).text()); 
       }); 
       alert(get_parestotal); 

      }); 

</script> 



<? echo json_encode($get_parestotal); ?> 
+0

你不能這樣做。但你可以alert或console.log(get_parestotal) – andrex 2014-09-22 12:43:00

+1

echo是在PHP服務器端,而變量是在客戶端... – fast 2014-09-22 12:43:19

+0

對。 你可以鏈接我一些使用console.log的例子嗎? 謝謝 – gatuso 2014-09-22 12:44:17

回答

2

您可以在控制檯登錄變量等。

如:

console.log(get_parestotal); 

你甚至可以拼接witht他用+ 的console.log的( '這是你的變量' + get_parestotal)

,你甚至可以添加樣式到您的日誌(顏色,背景顏色,...):

console.log('%cThis is your variable'+get_parestotal+'!','color:green;'); 

還有一些console.log()可供選擇:

console.warn(); 
console.info(); 
console.error(); 
console.debug(); 
+1

你也可以使用多個參數,例如'console.log('bla',window,123)',如果你使用了一個好的瀏覽器,你可以使用輸出比字符串更好http://puu.sh /bJgmN/13103a5940.png – Philipp 2014-09-22 12:57:00

+0

Thanks guys guys 我沒有正確地解釋上述問題。我需要在div上顯示值。 – gatuso 2014-09-22 17:14:06

0

如果你想要一個控制檯輸出比使用console.log(get_parestotal);