Is there a way to capture the alert ok button click event?

后端 未结 6 1447
广开言路
广开言路 2020-12-09 11:49

Is there a way to capture the alert ok button click event? In jQuery?

6条回答
  •  执念已碎
    2020-12-09 12:23

    The alert() function is synchronous and you can't verify what was clicked (it does not return anything), so the code below the call will be executed after it is closed (ok or close button). The alert is not used to gain user input. It is an alert, a message to the user. If you need to check what the user want, you should use confirm(). Note that the function name tells its purpose like alert.

    Something like:

    // if the ok button is clicked, result will be true (boolean)
    var result = confirm( "Do you want to do this?" );
    
    if ( result ) {
        // the user clicked ok
    } else {
        // the user clicked cancel or closed the confirm dialog.
    }
    

提交回复
热议问题