我想传递四个参数事件:$row['title']、$row['username'] 和 $row['date']。
<?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>
最佳答案
将函数参数的双引号更改为单引号
onclick="increasevotes(event,'chennai','venkat','01/07/2017')"
或使用 \
转义引号。
关于javascript - 为什么下面的 onclick 函数没有执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44983825/