【问题标题】:Javascript confirm & while looped echoJavascript 确认 & while 循环回显
【发布时间】:2013-03-11 05:58:05
【问题描述】:

我有一个小问题,我用 PHP while 循环制作了一个删除按钮,如下所示:

while($something = mysql_fetch_array($sql_something)){

    $id = $something['id']
    echo '<a href="somewhere.php?id='.$id.'"><button onclick="delconfirm()">Delete</button></a>

}

这个回声是一些内容的一些删除按钮。但是我需要用户确认才能删除,这就是 onclick="delconfirm()" 的用武之地。

我的确认是这样的:

function delconfirm()
{
    var r=confirm("Are you sure you want to delete this content?");

    if (r==true){

        // ...do nothing i guess? it needs to redirect using the PHP echo'd link...

    }
    else{

        window.location = "edit.php";

    }
}

但是,无论您按取消还是确定,它都会将其删除。我该如何解决这个问题?

【问题讨论】:

  • 请注意,mysql_* 函数已被弃用(请参阅red box)。
  • 您在&lt;a&gt; 中有一个&lt;button&gt;?这似乎很奇怪。你的整个表格是什么样子的?
  • r==true 是对== 的相当无意义的使用; r 在布尔上下文中评估应该得出相同的结果。如果您确实需要与true 进行精确比较,请使用r === true。
  • 您需要退货。 onclick='return delconfirm();'> 然后从 delconfirm() 中返回一些东西;
  • 是的,你有一个button 在a 中的原因吗?即使出于造型目的,我也无法想象让它们像这样嵌套的原因。无论如何,不​​要用button 处理onclick 事件 - 用a 处理所有事情 - 事件会冒泡,你可以阻止a 的行为(或不),具体取决于@ 987654339@

标签: php javascript while-loop confirm


【解决方案1】:

改成这样:

while($something = mysql_fetch_array($sql_something)){

    $id = $something['id']
    echo '<a href="somewhere.php?id='.$id.'"><button onclick="return delconfirm();">Delete</button></a>

}

然后是你的函数:

function delconfirm()
{
    return confirm("Are you sure you want to delete this content?");
}

编辑:如果您想要更多unobtrusive解决方案:

while($something = mysql_fetch_array($sql_something)){

    $id = $something['id']
    echo '<input type="button" value="Delete" data-id="$id" />';

}

然后是一些 javascript 来绑定事件:

function bindButtons() {
    var buttons = document.getElementsByTagName("input");
    for (var i = 0; i < buttons.length; i++) {
        if (buttons[i].type == "button") {
            buttons[i].onclick = function () {
                location.href='somewhere.php?id=' + this.getAttribute("data-id");
            }
        }
    }
}

并按照 Ian 的建议将其绑定到 window.onload:

window.onload = bindButtons;

注意:如果您使用 jQuery,此解决方案会更简单、更优雅。

Working jsFiddle

【讨论】:

  • @MarcelKorpel 从技术上讲,我认为来自内联 onclick 处理程序的 return false 将停止传播,因此甚至不让 &lt;a&gt; 知道点击了任何内容......因此工作
  • 不错的不显眼的建议 :) 也许在 Javascript 中使用 window.onload = bindButtons; 而不是 &lt;body onload="bindButtons();"&gt;。应该完成同样的事情,但更明确地将 HTML 和 JS 分开
【解决方案2】:

您需要停止/删除当前的点击事件。执行您的代码后,事件会到达锚点并触发点击。使用 MooTools 只需添加 'new Event().stop();'。我认为jQuery也有类似的东西。

编辑:Hanlet Escaño 是对的。可以返回true(浏览器会重定向到href中的URL,或者false让浏览器什么都不做)

【讨论】:

  • “汇”?正确的说法是“事件冒泡”,点击已经触发了
【解决方案3】:

为了防止 HTML 链接正常工作,您必须在 js 函数或 event.preventDefault() 中返回 false,其中 event 是传递给点击事件函数的参数

在 a 元素上而不是 a 标签内的元素上放置点击事件时,我做得很薄。但它可能会起作用。

【讨论】:

    【解决方案4】:

    如果用户按下取消,那么您需要阻止事件执行它通常会执行的操作。试试这个,例如:

    function delconfirm(e) {
        e = e || window.event;
    
        if (!confirm("Are you sure you want to delete this content?")) {
            e.preventDefault();
    
            // This will prevent the event from bubbling up to the <a>.
            e.stopPropagation();
    
            return false; // For the ancient/crappy browsers still out there.
        }
    
        return true;
    }
    

    【讨论】:

    • 为了使任何return 起作用,这意味着按钮也需要return - onclick="return delconfirm();"
    • @Ian 对。值得庆幸的是,事件对象上的两个方法调用应该对于任何现代浏览器都足够了。
    • 哈哈,正是……希望他们足够了:)
    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 2016-05-29
    • 2016-01-11
    • 2014-12-14
    • 2018-02-19
    • 2011-03-31
    • 1970-01-01
    相关资源
    最近更新 更多