how to develop yes no confirmation using jquery

前端 未结 3 1069
遇见更好的自我
遇见更好的自我 2021-01-04 18:55

How to develop confirmation dialog with yes no button using jquery or any other method ? I need that confirmation when I click on submit button.

3条回答
  •  灰色年华
    2021-01-04 19:09

    This will give you both a native JS and jQuery version:

    function confirm_action( strMessage ) {
        strMessage = (typeof strMessage !== 'undefined') ? strMessage : 'Are you sure you want to do this?';
        return !!confirm( strMessage );
    }
    
    $.fn.confirm_action = function ( strMessage ) {
        $(this).click( function(){
            return confirm_action( strMessage );
        });
    };
    

    Two different ways to use it. These work on links or Submit inputs.

    JavaScript:

    
    

    jQuery:

    $('#mybutton').confirm_action();
    

    Note that in either version you can customize the confirmation message by passing a string parameter.

提交回复
热议问题