繁体   English   中英

确认对话框在“取消”按钮上发布?

[英]Confirm dialog posting on Cancel button?

我正在使用以下<a>标记来显示一个简单的确认信息。 但是,当我单击“取消”按钮时,它仍然执行post方法。 根据我的理解,如果单击“取消”按钮,则在确认前加上return的内容将导致表单无法发布。

<a href="#" 
   data-potr-action="delete"
   data-potr-val="@item.RID"
   onclick="return confirm('Are you sure you want to delete this Request?');"
   class="btn btn-default btn-sm">
    <i class="glyphicon glyphicon-trash"></i> Delete
</a>

我在页面末尾有此代码,但我认为这与问题无关。 我不认为在“确认”对话框中选择“取消”时会触发click事件。

这只是将数据操作和数据值中的值存储到一个隐藏字段中。 这是在不应该点击的点击上完成的。

@section scripts {
    <script>
    $(document).ready(function () {
      // Hook events on buttons and anchors
      buildClickEvents();
    });

    // Hook events on buttons and anchors
    function buildClickEvents() {
      $("[data-potr-action]").on("click", function (e) {
        e.preventDefault();

        $("#EventCommand").val(
            $(this).data("potr-action"));

        $("#EventArgument").val(
            $(this).attr("data-potr-val"));

        $("form").submit();
      });
    }
    </script>
}

要回答您的问题,请先确认后再确认。 如果按下“确定”,则返回true;如果按下“取消”按钮,则返回false。 W3c看到这个

您可能会如下所示:

function buildClickEvents() {
  $("[data-potr-action]").on("click", function (e) {
    e.preventDefault();
    var result = confirm('Are you sure you want to delete this Request?');
    if(result == false){
       return;
    }
    $("#EventCommand").val(
        $(this).data("potr-action"));

    $("#EventArgument").val(
        $(this).attr("data-potr-val"));

    $("form").submit();
  });
}

并从标签中删除以下内容:

onclick="return confirm('Are you sure you want to delete this Request?');"
  $("[data-potr-action]").on("click", function (e) {
    if(confirm("your message")){
        $("#EventCommand").val(
            $(this).data("potr-action"));

        $("#EventArgument").val(
            $(this).attr("data-potr-val"));

        $("form").submit();
    }

    e.preventDefault();


  });

暂无
暂无

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

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