如何在PHP中添加JS确认到动态创建的按钮

时间:2016-06-06 17:28:17

标签: javascript php html php-5.3

我的应用动态生成href。如何添加JS确认onClick? 到目前为止,我已经尝试过这个:

echo'<a type="button" onClick="return confirm("Are you sure?")" 
class="btn btn-danger" href="delete.php?id='.$this->articleId.'" >Delete</a>';

它不起作用。任何想法如何解决它?谢谢:))

2 个答案:

答案 0 :(得分:1)

试试这个:

echo'<a type="button" onClick="return confirm(\'Are you sure?\')"
class="btn btn-danger" href="delete.php?id='.$this->articleId.'" >Delete</a>';>';

答案 1 :(得分:0)

请注意,"正在破坏您的代码:

onClick="return confirm("Are you sure?")" 

该行实际上分为三个:

onClick="return confirm(" //Ends here because the code is from " -> "
Are you sure? //This is nothing to the code and cannot be run
")" //This is another string

所以正确的做法如下:

onClick="return confirm('Are you sure?')"