简体   繁体   English

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

[英]Confirm dialog posting on Cancel button?

I'm using the following <a> tag to display a simple confirm which it does. 我正在使用以下<a>标记来显示一个简单的确认信息。 However, when I click the Cancel button it still performs the post method. 但是,当我单击“取消”按钮时,它仍然执行post方法。 From my understanding, having the return in front of confirm should cause the form to not post if the Cancel button is clicked. 根据我的理解,如果单击“取消”按钮,则在确认前加上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>

I have this code at the end of the page but I don't think it has anything to do with the issue. 我在页面末尾有此代码,但我认为这与问题无关。 I didn't thinking it would fire the click event when selecting Cancel in the Confirm dialog. 我不认为在“确认”对话框中选择“取消”时会触发click事件。

This just takes the values in the data-action and data-value and stores them to a hidden field. 这只是将数据操作和数据值中的值存储到一个隐藏字段中。 This is done on click which it shouldn't be getting to. 这是在不应该点击的点击上完成的。

@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>
}

To answer your question no confirm doesn't block form post. 要回答您的问题,请先确认后再确认。 It return true if pressed "OK" or false if pressed "Cancel" button. 如果按下“确定”,则返回true;如果按下“取消”按钮,则返回false。 See this from W3c W3c看到这个

What you could is as below: 您可能会如下所示:

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();
  });
}

And remove the below from a tag: 并从标签中删除以下内容:

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