Home > Web Front-end > JS Tutorial > jquery ui dialog replaces confirm instance analysis_jquery

jquery ui dialog replaces confirm instance analysis_jquery

WBOY
Release: 2016-05-16 15:18:10
Original
1425 people have browsed it

The example in this article describes how jquery ui dialog replaces confirm. Share it with everyone for your reference, the details are as follows:

Some browsers will directly block the confirm function of js, making the function unusable. It is recommended to use the dialog function of jquery ui to perfectly replace the confirm function

1. html code

<div id="confirm_dialog" title="提示" style="display:none;">
</div>

Copy after login

Put the above code in a public place

2. Simulate confirm js code

var common = {
  confirm_act:function(dialog_id,msg,callback) {
    $("#"+dialog_id).html("<p class='message'>"+msg+"</p>");
    $("#"+dialog_id).dialog({
      resizable: false,
      modal: true,
      overlay: {
        backgroundColor: '#000',
        opacity: 0.5
      },
      buttons: {
        '确认': function() {
          callback.call();
          $(this).dialog('close');
        },
        '取消': function() {
          $(this).dialog('close');
        }
      }
     });
  }
}

Copy after login

A method confirm_act is defined and placed in a public js file. The first parameter is the ID of the elastic layer, the second parameter is the prompt message, and the third parameter is the callback function.
Note that when calling the callback function, you must use the call() function of js. This callback function can take parameters or include a callback function.

3. Callback js code

var recommend = {
  delete: function(url,obj)
  {
    $.ajax({
      url: url,
      type: "get",
      success:function(data)
      {
        ............省略..........
      }
    });
  }
}

Copy after login

4. How to call

$('.recommended_delete').click(function(){
  var obj = this;  //重命名
  common.confirm_act('confirm_dialog',$(obj).attr('msg'),function(){recommend.delete($(obj).attr('url'),obj)});
});

Copy after login

Note that if you want to pass this in the function, be sure to redefine .

Readers who are interested in more jQuery-related content can check out the special topic on this site: "Summary of jQuery common plug-ins and usage"

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template