在JavaScript中返回确认窗口

时间:2016-04-06 05:53:19

标签: javascript html

我正在使用if else语句,该语句仅根据语句显示。我的确认窗口无效。它只会在没有确认的情况下进入注销页面。

我的代码:

else
{
    echo "
              <td class='td1' align='center'>
                  <a href='logout.php' onclick='return confirm('Are you sure you want to Log Out?')'><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'></a>
              </td>
          </tr>
      </table>

    ";}

5 个答案:

答案 0 :(得分:1)

你要么用

&#13;
&#13;
<a href="whatever" onclick="return confirm('are you sure?')"><img ...></a>
&#13;
&#13;
&#13;

注意返回(确认):内部事件中脚本返回的值决定是否运行默认浏览器操作;如果您需要运行大量代码,您当然可以调用另一个函数:

&#13;
&#13;
<script type="text/javascript">
    function confirm_delete() {
      return confirm('Are you sure?');
    }
</script>
...
<a href="whatever" onclick="return confirm_delete()"><img ...></a>
&#13;
&#13;
&#13;

否则你可以试试这个:

&#13;
&#13;
onclick="javascript:return confirm('Are you sure you want to logout?')"
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只需简单地用您的代码替换代码,因为您在确认之前已经放置了一个返回,但是通过JavaScript有一个confirm关键字,它将为您提供您期望的功能,而不是返回确认。只需确认即可。

&#13;
&#13;
else
{
    echo "
              <td class='td1' align='center'>
                  <a href='logout.php' onclick='confirm('Are you sure you want to Log Out?')'><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'></a>
              </td>
          </tr>
      </table>

    ";}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

<table><tr>
<td class='td1' align='center'>
<a href='logout.php' onclick="return confirm('Are you sure you want to Log Out?')">Log Out</a>
</td>
</tr>
</table>

将您的代码更改为此

else
{
echo "
<td class='td1' align='center'>
<a href='logout.php' onclick=\"return confirm('Are you sure you want to Log Out?')\"><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'></a>
</td>
</tr>
</table>

";}
  1. 在onlclick中使用转义序列。

答案 3 :(得分:0)

使用双""代替''。那好吧。更改为onclick="confirm('Are you sure you want to Log Out?')"

<a href="logout.php" onclick="confirm('Are you sure you want to Log Out?')"><img src='./buttons/logout1.png' width='50' height='35' alt='Log Out'> </a>

答案 4 :(得分:-1)

小心单引号。在确认中将´替换为"

以下是一个例子:jsfiddle.net/37z6wpmz /

相关问题