2017-07-08 30 views
0

我想傳遞四個參數event $ row ['title'],$ row ['username']和$ row ['date']。爲什麼以下onclick函數沒有執行?

<?php 
echo "<script> 
    function increasevotes(e,location,user,date) 
    { 
    e.preventDefault(); 
    console.log(\"Hi!\"); 
    } 
    </script>"; 

//I've proper code here for fetching the mySQL query results into $rows and that part is working perfectly. 

while($rows=mysqli_fetch_array($result)) 
{ 
if($rows['public']=="yes") 
{echo "<span class=\"right\" id=\"nolikes\">{$rows['vote']}</span><a href=\"\" onclick=\"increasevotes(event,\"{$rows['title']}\",\"{$rows['username']}\",\"{$rows['date']}\")\"><img src=\"img/like.png\" class=\"right\" width=\"30\" height=\"30\"/></a>"; 
} 
?> 

什麼是實際正在做的事情,頁面正在被刷新,而不是。 當我點擊查看源代碼頁,這是圖所示:

<span class="right" id="nolikes">0</span> 
<a href="" onclick="increasevotes(event,"chennai","venkat","01/07/2017")"><img src="img/like.png" class="right" width="30" height="30"/> 
</a> 
+0

如果你把一個console.log('腳本正在運行');在你的函數之前,所以它會在頁面加載時運行...你看到這條消息嗎? – TimBrownlaw

+0

檢查錨標記元素,你會發現它爲什麼不起作用 – Nagaraju

+2

ID必須是唯一的 – Andreas

回答

1

變化雙引號功能參數來單引號

onclick="increasevotes(event,'chennai','venkat','01/07/2017')" 

或使用\逃跑的報價。

+0

現在它的工作,謝謝。爲什麼它不早? – cyberdon