2014-09-22 55 views
1

我正在給定SSCCE代碼以下錯誤:的ReferenceError:電子沒有定義

Error: ReferenceError: electron is not defined 
Source File: http://localhost/testingOne.php 
Line: 1 

雖然'electron'是一個字符串。那麼爲什麼我會得到這個錯誤。而我試圖接受的答案here"包裹electronproton但給出:

Error: SyntaxError: syntax error 
Source File: http://localhost/testingOne.php 
Line: 1, Column: 12 
Source Code: 
clickButton(

代碼:

<?php 

echo '<html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <script> 
     function clickButton(alp, bet) { 
      alert(alp); 
      alert(bet); 
     } 
    </script> 
</head><body>'; 

$array = array('alpha'=>'electron', 'beta'=>'proton'); 

echo '<button type="button" onclick="clickButton('. $array['alpha'].','.$array['beta'] .')" >Click Me</button>'; 

echo '</body></html>'; 
?> 

回答

2

如果你查看源代碼的實際HTML創建,你'd see:

<button type="button" onclick="clickButton(electron,proton)" >Click Me</button> 

您需要添加引號周圍輸出值將它們轉換成JavaScript字符串文字:

echo '<button type="button" onclick="clickButton(\''. $array['alpha'].'\',\''.$array['beta'] .'\')" >Click Me</button>'; 

給:

<button type="button" onclick="clickButton('electron','proton')" >Click Me</button>