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>
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'); } } }); } }
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) { ............省略.......... } }); } }
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)}); });
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.