在<a> element

时间:2016-08-13 21:33:54

标签: javascript html

I have an a element containing an href attribute. Clicking on it would delete data so I want the user to confirm that action. The href attribute refers to a php file with an id of the data that will be deleted in the GET parameter. I've added an onclick attribute, that should execute the following piece of JS (it shows a Semantic UI modal that asks for confirmation):

    confirmmodal = function () {
    beforeunload = function () {
        $('.ui.basic.modal')
            .modal({
                closable: false,
                onDeny: function () {
                    return false;
                },
                onApprove: function () {
                    return true;
                }
            })
            .modal('show')
        ;
}
}

But when I run this it still goes to the page that would delete the data (although I haven't built it yet, so nothing is deleted). Would there be an option that gives the onclick attribute priority over the href attribute somehow?

2 个答案:

答案 0 :(得分:1)

您需要在代码末尾添加event.preventDefault()

例如:

self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2)

答案 1 :(得分:0)

好的,我在剧本上进行了一些调整,并考虑了gavgrif的评论。

我使<a>元素略有不同,因此它不再包含href属性:

<a title="Delete post"  onclick="confirmmodal(this)" data-postid="'. $row['postnr'] .'"><i class="large delete middle aligned icon"></i></a>

现在,如果单击图标,postid也可用于JS,因此我们可以在单击确认按钮时参考GET参数:

confirmmodal = function (a) {
    $('.ui.basic.modal')
        .modal({
            closable: false,
            onDeny: function () {
                return true;
            },
            onApprove: function () {
                window.location.href = "deletepost.php?id=" + a.dataset.postid
                return true;
            }
        })
        .modal('show')
    ;
}

这是一个半丑陋的修复,但它不是那么多行,我不知道关于JQuery的***:)

感谢所有的帮助,我几乎与preventDefault()一起到达那里但如果行动得到确认我就无法继续,所以这是一个更容易的解决方案。