javascript - 为什么下面的 onclick 函数没有执行?

标签 javascript php

我想传递四个参数事件:$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/

相关文章:

php - 有些行在页面及其源代码中时未显示

javascript - 如果链接为空,如何隐藏图标

javascript - ASP.NET 3.5 AJAX 控件工具包 PopupControlExtender

php - 如何清除 php 链接中的参数?

php - 在搜索 PEAR HTML_Table 替换

php - 如果我单击选择按钮并将其值传递到文本框中,如何获取所选单选按钮的值?

javascript - 如何在 JS "onclick"中使用 "setAttribute"运行函数?

javascript - Node.js 创建一个模块来收集内存(ram)信息?

javascript - 使用简单数组的 ng-options 将索引放入模型

php - 如何使用 PHP 加快查询速度?