How to create a custom “confirm” & pause js execution until user clicks button?

前端 未结 6 909
后悔当初
后悔当初 2020-12-11 01:37

Ok, I\'m doing a bunch of RIA/AJAX stuff and need to create a \"pretty\", custom confirm box which is a DIV (not the built-in javascript confirm). I\'m having trouble determ

6条回答
  •  青春惊慌失措
    2020-12-11 02:22

    The way how I did this:

    1. Create your own confirm dialog box with buttons, let's say "Yes" and "No".
    2. Create function that triggers the dialog box, let's say confirmBox(text, callback).
    3. Bind events on "Yes" and "No" buttons - "Yes" - callback(true), "No" - callback(false).
    4. When you are calling the function use this syntax:

      confirmBox("Are you sure", function(callback){
          if (callback) {
              // do something if user pressed yes
          } 
          else {
              // do something if user pressed no
          }
      });
      

提交回复
热议问题