简体   繁体   English

使用Jquery和MVC4自定义确认

[英]Custom Confirm With Jquery And MVC4

Very new to jquery and trying to implement a confirm box for a delete button. jQuery的新增功能,它试图为删除按钮实现一个确认框。 I can get a simple standard alert box to perform the function i want with this code: 我可以使用以下代码获得一个简单的标准警报框来执行我想要的功能:

<script type="text/javascript">
    $(document).ready(function () {
        $("input[name='deleteComment']").click(function () {
            return confirm('Are You Sure You Want To Delete This?');
        });
    });
</script>

Now this is functional but looks horrible so i am trying to use jQuery-confirmOn plugin. 现在这是可以正常运行的,但是看起来很可怕,所以我正在尝试使用jQuery-confirmOn插件。 Their documentation says to call it i should use code like this: 他们的文档说要调用它,我应该使用如下代码:

$('#myButton').confirmOn('click', function(e, confirmed){
    if(confirmed) deleteSomethingImportant();
    else {
        //If we need to, we can do some actions here if the user cancelled the confirmation
    }
})

With my very limited knowledge of jquery i can understand what this is doing just about. 以我对jquery的了解非常有限,我可以理解它到底在做什么。 But its the following line i need help with. 但是它的以下几行我需要帮助。

if(confirmed) deleteSomethingImportant();

what would i need to edit that line to to have the same effect as my initial example? 我需要如何编辑该行才能产生与最初示例相同的效果? I struggle to understand how jquery will interact with MVC so any help would be appriciated. 我很难理解jquery将如何与MVC交互,因此任何帮助都将得到应用。

Thanks in advance 提前致谢

You could just write a JavaScript function to create a simple confirm dialog with jQueryUI. 您可以编写一个JavaScript函数以使用jQueryUI创建一个简单的确认对话框。 It's really simple. 真的很简单。

You pass the message to display on the dialog and callback to call when "Yes" button is clicked. 您传递消息以显示在对话框上,并在单击“是”按钮时回调以进行调用。

function confirm(message, callback) {
    $('<div></div>').appendTo('body')
        .html('<div><p>' + message + '</p></div>') // Append the message
        .dialog({
            resizable: false,
            autoOpen: true,
            modal: true,
            buttons: [{
                text: "Yes",
                click: function () {
                    // Callback on click
                    if (typeof(callback) !== "undefined") {
                         callback();
                    }
                    $(this).dialog('close');
                }
            },
            {
                text: "No",
                click: function () {
                    $(this).dialog('close');
                }
            }]
    });
}

Now say you want to confirm posting a form. 现在说您要确认发布表单。 You just call the function with the message and callback parameters: 您只需使用message和callback参数调用该函数:

$('#myButton').click(function(e) {
    e.preventDefault();

    // Prompt the submit
    confirm("Are you sure you want to delete this?", function() {
        // Submit the form
        $("#myForm").submit();
    });
})

Somewhere in your html you have the form you want to post: 在html的某处,您具有要发布的表单:

@using ( Html.BeginForm("Delete", "MyController", FormMethod.Post, new {id="myForm"} ))
{
   ....
}

And receive it in "MyController" 并在“ MyController”中接收它

[HttpPost]
public ActionResult Delete(SomeModel model)
{
    /* Delete the entity from database */
    ....

    return RedirectToAction("Index");
}

Try like this, it's just an example 像这样尝试,这只是一个例子

$(document).ready(function(){

    $(".confirm").easyconfirm();
    $("#alert").click(function() {
        alert("You approved the action");
    });
});


<a href="#" class="confirm" id="alert">Normal test</a>

And for more examples read this 有关更多示例,请阅读此内容

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

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