ASP.NET MVC - 有一个表格确认按钮 [英] ASP.NET MVC - Having a confirmation button with a Form

查看:114
本文介绍了ASP.NET MVC - 有一个表格确认按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个强类型的视图我的模型和我想的是,当用户点击提交,确认对话框弹出确认用户确实想提交表单,如果他们单击取消则应该不会触发该视图的 HttpPost 动作,这可能吗?

I have an strongly typed view for my model and what I'd like is that when the user clicks on submit, a confirmation box pop up confirming that the user does indeed wish to submit the form, if they click cancel then it shouldn't fire the HttpPost Action for that View, is this possible?

推荐答案

当然,这是可能的。我喜欢用一个不显眼的方式。下面是一个简单的例子:

Of course it is possible. I like to use an unobtrusive approach. Here is a simplified example:

jQuery(document).ready(function () {
  jQuery('[data-confirm]').click(function (e) {
    if (!confirm(jQuery(this).attr("data-confirm")))
    {
      e.preventDefault();
    }
  });
});

然后你只需要一个数据确认属性添加到您的提交按钮例如

Then you only need to add a data-confirm attribute to your submit button for example

<input type="submit" data-confirm="are u sure?" />

当然你也可以使用上的链接,按钮等你是不是仅限于提交按钮,此属性,如果你想在以后实现一个票友确认对话框,你将不得不更换code仅在一个地方。

Of course you can use this attribute on links, buttons, etc. you are not restricted to submit buttons only, and if you want to implement a fancier confirm dialog later than you will have to replace the code only in one place.

这篇关于ASP.NET MVC - 有一个表格确认按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆