jquery dialog: confirm the click on a submit button
You need to have your dialog button submit the <form>
, like this:
'Delete all items': function() {
$(this).dialog('close');
$("#myForm").submit();
}
The rest of your code is correct, but it's currently just closing the dialog and returning, you need to actually submit the form. Also, it's better to do this as a click handler, instead of onclick
, like this (updated for comments):
var currentForm;
$(function() {
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
autoOpen: false,
buttons: {
'Delete all items': function() {
$(this).dialog('close');
currentForm.submit();
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$(".delete").click(function() {
currentForm = $(this).closest('form');
$("#dialog-confirm").dialog('open');
return false;
});
});
Then just give your <input>
an class of delete
, like this:
<input class="delete" type="submit" value="delete" />
For a form with GET
method as I need also button's value in query string and avoid to lost it at form submit
I used event.preventDefault()
and added button's name and value as hidden <input> field
in JS script
JS script
$( function() {
//Add function to all document's html tag with class="delete"
$(document).on("click", ".delete", function(event) {
event.preventDefault(); //Stop submitting form and wait for a "yes" or "no"
$( "#dialog-confirm" ).dialog({
draggable: false,
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Delete items": function() {
$( this ).dialog( "close" );
//Prevent to lost button's value in query string at form submit
//Need to add a hidden <input> field with the same button's name and value
$('<input />').attr('type', 'hidden')
.attr('name', 'action')
.attr('value', 'delete')
.appendTo('#myform');
$('#myform').submit(); //Ok proceed with form submit
},
Cancel: function() {
$( this ).dialog( "close" ); //Do nothing, close window.
}
}
});
});
});
Form template
<div style="display:none" id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
<form id="myform" action="#myurl" method="get">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input class="delete" type="submit" name="action" value="delete">
</form>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Dialog - Modal confirmation</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$(document).on("click", ".delete", function(event) {
event.preventDefault(); //Stop submitting form and wait for a "yes" or "no"
$( "#dialog-confirm" ).dialog({
draggable: false,
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Delete items": function() {
$( this ).dialog( "close" );
//Prevent to lost button's value in query string at form submit
//Need to add a hidden <input> field with the same button's name and value
$('<input />').attr('type', 'hidden')
.attr('name', 'action')
.attr('value', 'delete')
.appendTo('#myform');
$('#myform').submit(); //Ok proceed with form submit
},
Cancel: function() {
$( this ).dialog( "close" ); //Do nothing, close window.
}
}
});
});
});
</script>
</head>
<body>
<div style="display:none" id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
<form id="myform" action="#myurl" method="get">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input class="delete" type="submit" name="action" value="delete">
</form>
</body>
</html>
If you're using jQuery, then do it properly and avoid inline Javascript:
$(function (){
$("form").submit(function (){
// return false if you don't want this to be submitted
// maybe do a
// return ask();
// but with the code provided it doesn't seem
// to work like that - ask() is not returning anything at all!
});
});