简体   繁体   English

链接不要求用户确认

[英]Link not asking user to confirm

I have this code 我有这个代码

echo '<div class="bottomright"><a href="?id='
                . $topic_id
                . '&part=5"><img src="../assets/icons/Comments-    edit.png" /></a><a href="?id='
                . $topic_id
                . '&part=6"><img src="../assets/icons/Lock.png" /></a><a href="#" onclick="return confirm("Are you sure?")"><img src="../assets/icons/Trash.png" /></a></div>'; 

When I click Trash.png it is supposed to ask "Are you sure"? 当我单击Trash.png时,应该问“您确定”吗? But it doesn't... 但这不是...

Am I missing something here? 我在这里想念什么吗?

Here's the problem: 这是问题所在:

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

Note the quote ambiguity. 请注意报价含糊之处。 You have to change the quote symbols somehow to pair properly, probably by escaping a set of single quotes. 您必须以某种方式更改引号符号以使其正确配对,这可能是通过转义一组单引号引起的。 Both of the following work: 以下两项工作:

onclick="return confirm(\\'Are you sure?\\')"

onclick=\\'return confirm("Are you sure?")\\'

<a href="#" onclick="return confirm(\'Are you sure?\')">

You have to escape single quotes. 您必须转义单引号。

Because: 因为:

  • onclick="return confirm("Are you sure?")" is not JavaScript valid. onclick="return confirm("Are you sure?")"无效的JavaScript (notice " conflict) (通知"冲突)

  • onclick="return confirm('Are you sure?')" would not be PHP valid (notice ' conflict because the whole echo function is wrapped inside single quotes) onclick="return confirm('Are you sure?')"将不是PHP有效的(注意'冲突,因为整个echo函数都包装在单引号内)

Another option, would be to do this: 另一种选择是这样做:

'<a href="#" onclick="return confirm('. "'Are you sure?'" .')">'

Which actually closes the echo quotes, concatenates the 'Are you sure?' 哪一个实际上关闭了echo引号,将'Are you sure?'串联起来'Are you sure?' and concatantes the rest. 并保持其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM