简体   繁体   English

将确认/取消弹出窗口插入JQuery-CSS问题

[英]Inserting a Confirm/Cancel pop-up into JQuery - CSS Issue

I am still quite new to JQuery and I am trying to make a simple pop-up message to confirm a delete, but I want the table row to turn red during this process. 我对JQuery还是很陌生,我正在尝试制作一条简单的弹出消息以确认删除,但是我希望表行在此过程中变为红色。

I found this code that seems sweet, short, and simple. 我发现这段代码看起来很甜美,简短而简单。

$(document).ready(function(){
    $('.confirm').click(function(){
        var answer = confirm("Are you sure you want to delete this item?");
        if (answer){
            return true;
        } else {
            return false;
        }
    });
});

from: http://brettgregson.com/programming/how-to-make-a-are-you-sure-pop-up-with-jquery/138 来自: http : //brettgregson.com/programming/how-to-make-a-are-you-sure-pop-up-with-jquery/138

And I am "attempting" to add it onto my current deleteFunction(), but I am still pretty new to JQuery and I am having some "bugs" with it. 而且我正在“尝试”将其添加到当前的deleteFunction()中,但是我对JQuery还是很陌生,并且我遇到了一些“错误”。

My deleteFunction (no confirmation - but color updating works fine) 我的deleteFunction(没有确认-但颜色更新正常)

function deleteFunction(element) {
        var newID = $(element).closest("td").find("span.ID").text();
        $(element).closest("tr").css('background-color', 'red');
        $.post(
                '@Url.Action("customDelete", "Movie")',
                 {
                     'id': newID
                 },
                function (data) { },
                "json"
            );
        $(element).closest("tr").hide();
    }

My insertion of the confirmation box works, but does not update the tr background color, nor does it revert the color back to the default upon cancellation. 我插入的确认框有效,但不会更新tr背景色,也​​不会在取消时将颜色恢复为默认值。

function deleteFunction(element) {
    var newID = $(element).closest("td").find("span.ID").text();
    $(element).closest("tr").css('background-color', 'red');

    $(document).ready(function () {

        var answer = confirm("Are you sure you want to delete this item?");
        if (answer) {
            $.post(
        '@Url.Action("customDelete", "Movie")',
        {
            'id': newID
        },
        function (data) { },
        "json"
        );
            $(element).closest("tr").remove();
            return true;
        } else {
            $(element).closest("tr").css('background-color', 'default');
            return false;
        }
    });


}

If someone could explain why the CSS color is not being touched until after the confirmation box appears (or why it does not remove the color after Cancel is pressed) it would be very much appreciated. 如果有人可以解释为什么直到确认框出现后才触摸CSS颜色(或者为什么在单击“取消”后它不删除颜色),将不胜感激。

I've created a jsfiddle for you: working sample . 我为您创建了一个jsfiddle: 工作示例 As for clearing the color, you should use css('background-color', 'initial') . 至于清除颜色,您应该使用css('background-color', 'initial') As for highlighting - it should highlight, as sample does. 至于突出显示-它应该像示例一样突出显示。 If it does not help, then feel free to reveal your html markup, most likely the issue is there 如果没有帮助,请随时显示您的html标记,很可能是问题所在

You can call your delete function if user wants to delete, like this way 如果用户要删除,则可以调用delete函数,就像这样
here is your delete function: 这是您的删除功能:

function deleteFunction(element) {
    var newID = $(element).closest("td").find("span.ID").text();
    $(element).closest("tr").css('background-color', 'red');
    $.post(
            '@Url.Action("customDelete", "Movie")',
             {
                 'id': newID
             },
            function (data) { },
            "json"
        );
    $(element).closest("tr").hide();
}


and here is cofirm to delete: 这里是确认删除:

$(document).ready(function(){
  var ans=confirm("Are you sure you want to delete this item?");
 if(ans)
    {
     deleteFunction(element);
     }
   else
    {
        return false;
     }
});

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

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