1
echo "<html>";
echo "<head>";
echo "<script>";
echo "  function logout()";
echo "  {";
echo "  var r=confirm(\"Are you sure you want to logout?\");";
echo "  if (r==true){window.location.href=\"http://www.google.com\";}";
echo "  }";
echo "</script>";
echo "</head>";
echo "<body>";
echo "<div ALIGN=\"right\" onclick=\"logout();\"> <a href=\"\">Logout </a> </div>";
echo "</body>";
echo "</html>";

从上面的代码中,我只想将用户重定向到www.google.com一旦用户在确认框中单击“是”。我试图在 ( r == true) 之后立即发出警报,它可以工作,但是页面没有转到www.google.com. 我可以知道我错过了什么吗?

4

2 回答 2

5

好吧,我认为您应该添加类似这样的代码:

echo "<div ALIGN=\"right\" onclick=\"logout();\"><a href=\"javascript:return;\">Logout </a> </div>";
因为在触发 DIV 元素上的单击事件绑定后页面已刷新。

于 2013-09-10T02:50:17.840 回答
0

如果您有 Jquery 和 Jquery UI,这将起作用

    echo "<html>";
    echo "<head>";
    echo "<script>";
    echo '$( "#dialog-logout" ).dialog({
        autoOpen: false, 
        resizable: false,
        height:160,
        modal: true,
        buttons: {
            "Logout": function() {
            location.href = "http://www.google.com";
                $( this ).dialog( "close" );},
            Cancel: function() {
                $( this ).dialog( "close" );}}});
        $( "#logout" )
        .click(function() {
        $( "#dialog-logout" ).dialog( "open" );});'

    echo "</script>";
    echo "</head>";
    echo "<body>";


    echo "<div ALIGN=\"right\" id=\"logout\"> <a href=\"\">Logout </a> </div>";
    echo '<div id="dialog-logout" title="Logout">
            <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>You are about to logout. Are you sure?</p>
        </div>';

    echo "</body>";
    echo "</html>";
于 2013-09-10T02:48:29.233 回答