0

我已经对此主题进行了一些研究,发现在 php 中使用确认框的方法之一是使用 javascript onclick()。我有这个不起作用的代码。

 echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'"  style="color:red" onclick="return confirm("Are you sure you want to delete this product ?")">Delete</a></td>';

我认为问题在于使用'and"但我不确定如何构造这个回声。当我在其中使用单引号时confirm('Are you sure you want to delete this product'),也会出现错误。有什么想法可以构造这个回声吗?谢谢

4

4 回答 4

2

您必须转义单个 qoute,这是您的编辑

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'"  style="color:red" onclick="return confirm(\'Are you sure you want to delete this product ?\')">Delete</a></td>';
于 2013-05-16T21:05:18.417 回答
1

最小的模组是这样的:

onclick="return confirm('Are you sure you want to delete this product ?')"

实例| 直播源

请注意,这是属性 ( )上的双引号和 JavaScript 代码上的引号(因为 JavaScript 支持对字符串使用单引号)。onclick

如果您需要在消息中包含撇号(这很常见),请记住属性的内容是 HTML 文本,并且在 HTML 文本中您可以使用 HTML 实体。所以这也有效:

onclick="return confirm(&quot;You're really sure want to delete this product ?&quot;)"

实时复制| 实时源 (我更改了消息,因此它包含一个'。)

尽管在这种情况下的另一种选择是使用转义的撇号:

onclick="return confirm('You\'re really sure want to delete this product ?')"

实时复制| 直播源

于 2013-05-16T21:03:10.890 回答
0

您只需要小心使用引号:PHP String Reference

echo '<td class="item_unsold"><a href = "manage-products.php?prod='.$row[0].'"  style="color:red" onclick="return confirm(\'Are you sure you want to delete this product ?\')">Delete</a></td>';

在这里,我已经转义了一组应该为您解决问题的引号。

于 2013-05-16T21:04:12.263 回答
0
enter code here echo '<td class="item_unsold"><a href = "manage-products.php?prod='.@$row[0].'"  style="color:red" onclick="return confirm(\'Are you sure you want to delete this product ?\')">Delete</a></td>';

尝试用反斜杠转义单引号

于 2013-05-16T21:06:49.893 回答