JavaScript confirm cancel button not stopping JavaScript

前端 未结 7 1479
天涯浪人
天涯浪人 2020-12-24 15:27

I have a delete button that is tied to some comments on a page i have. When you click the delete button i am trying to get a confirm dialog box to pop up asking if you are

相关标签:
7条回答
  • 2020-12-24 16:03
    onclick="if (confirm('Are you...?')) commentDelete(1); return false"
    

    You are missing an if. In your version, first you get a question, and then regardless of the answer, you call commentDelete.

    0 讨论(0)
  • 2020-12-24 16:03

    You should return false to prevent the default event. This should work:

    onclick="confirm('Are you sure that you want to delete this comment?'); commentDelete(1);return false;"
    
    0 讨论(0)
  • 2020-12-24 16:04
    function confirmCancel(){
       var msj='Are you sure that you want to delete this comment?';
       if (!confirm(msj)) { 
          return false;
       } else {
          window.location='backcables.php';
       }
    }
    
    0 讨论(0)
  • 2020-12-24 16:13

    Maybe it's because you set the type=submit in the form that contains the javascript.

    You should set it to button or image or whatever if you don't want to be submitted if clicked cancel

    <form name="form_name">
        <input type="button" onclick="javascript_prompt()" value="GO"/>
        <input type="hidden" name="new_name" value=""/>
    </form>
    

    example of javascript prompt:

    function javascript_prompt(){
        var name = prompt("Tell me your name.", "");
        if (name != "" && name != null){
            document.form_name.new_name.value = name;
            document.form_name.submit();
        }else{
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-24 16:17

    You are treating the confirm is if it is a if statement, it just returns a Boolean true or false.

    if(confirm('foo')){ alert('bar'); }
    
    0 讨论(0)
  • 2020-12-24 16:20

    In the head tag you can write following code:

    <script language="javascript" type="text/javascript">
    
        function getConfirmation()
        {
            var retVal = confirm("Do you want to continue ?");
            if (retVal == true)
            {
                alert("User wants to continue!");
                return true;
            } 
            else
            {
                alert("User does not want to continue!");
                return false;
            }
        }
    </script>
    

    After writing this code you can call this function in the following code:

    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
        CommandName="Edit" Text="Edit" **OnClientClick="getConfirmation()"**>
    </asp:LinkButton>
    
    0 讨论(0)
提交回复
热议问题