This is my first question on StackOverflow and I’ve already visited every single question about jQuery Dialog & Forms, but still have an issue.
I’m trying to implement a jQuery Confirmation Dialog on my form. The window actually appears without issues, but when I click “Confirm”, the form isn’t submitted…
This is the Javascript :
// Form validation
$(function(){
var currentForm;
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
'Confirm': function(e) {
currentForm.submit();
return true;
},
Cancel: function() {
$(this).dialog('close');
return false;
}
}
});
$("#news-listing").submit(function(e){
currentForm=$(this);
if($("#dialog-confirm").dialog("open"))
{
return false;
}
});
});
And there is the HTML
<div id="dialog-confirm" title="Empty the recycle bin ?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin: 20px 10px 20px 10px;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
<form action="news/action/delete" id="news-listing">
<table id="users-listing">
<tr>
<th class="id">ID</th>
<th class="title">Title</th>
<th class="url">URL</th>
<th class="modify">Modify</th>
<th class="checkcell">Delete</th>
</tr>
<tr class='alt'>
<td class='id'>".$news['news_id']."</td>";
<td class='title'>".$news['news_title']."</td>";
<td>".$this->News->get_url($news['news_id'])."</td>";
<td class='modify'><a href="modify/1">Modify</a></td>;
<td><input type="checkbox" name="delete[]" /></td>
</tr>
</table>
<input type="submit" name="submit" />
</form>
Thank you in advance for your help !
EDIT :
Here is my new code, the HTML does not change. Now I have the dialog, but confirm button doesn’t submit, cancel button works well.
$(function() {
// Get the current form object
var currentForm = $("#news-listing");
// Initialize dialog
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
'Confirm': function() {
currentForm.submit();
return true;
},
Cancel: function() {
$(this).dialog('close');
return false;
}
}
});
// Validate the form
$("#news-listing").validate({
rules:{
"delete[]":"required"
},
submitHandler: function(e){
$("#dialog-confirm").dialog("open");
}
});
});
The problem is that
currentFormisundefinedwhen this line is executed:An error will be thrown because
undefineddoes not have asubmitmethod!You attempt to set the value of
currentFormin thesubmitevent handler of the form itself, so you are suffering from something of a chicken and egg problem. You need to assign the form element tocurrentFormoutside of the event handler: