Skip to content

Instantly share code, notes, and snippets.

@oppara
Created September 20, 2009 13:57
Show Gist options
  • Save oppara/189800 to your computer and use it in GitHub Desktop.
Save oppara/189800 to your computer and use it in GitHub Desktop.
jQuery confirm plug-in
/*
* jQuery confirm plug-in
*
* @see http://blog.smartnetwork.co.jp/staff/jquery-confirm-plugin
*
* usage:
* <form id="hoge" method="post">
* <input type="submit" name="hoge" value="ダイアログなし"/>
* <input type="submit" name="hoge" title="ほんとに送信?" value="ダイアログあり"/>
* </form>
*
* $(document).ready(function(){
* $("#hoge").confirm();
* });
*
*/
(function($){
jQuery.fn.confirm = function(config){
config = jQuery.extend({
},config);
this.each(function(){
var form = this;
$(this).find(':submit').each(function(){
var name = $(this).attr('name');
var title = $(this).attr('title');
if(title){
$.data(this, 'dialog', {name: name, title:title});
}
});
$(this).submit(function(event){
if(! $.data(this, 'confirmed')){
var submit = $.data(form, 'clicked_button');
var dialog = $.data(submit, 'dialog') || $.data(form, 'dialog');
if ($(dialog).attr('name') == $(submit).attr('name')) {
var ret = confirm($(dialog).attr('title'));
if (!ret ) {
$.removeData(form, 'confirmed');
$.removeData(form, 'clicked_button');
}
return ret;
}
return true;
}
});
$(this).find(':submit').click(function(){
$.data(form, 'clicked_button', this);
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment