I have this code to wire up the confirmation dialog for delete links, which I trigger via POST.
Those links work fine when I have nothing to POST to the MVC controller action (no form elements). But when I have form elements whose values need to be POSTed back I of course need a submit button. I would also like to use similar script to wire up the confirmation dialog with a submit button too.
Essentially the dialog should pop up, wait for confirmation and if confirmed, continue with posting the form to the action.
/** Delete links wire up **/
var deleteLinkObj;
// delete Link
$('.delete-link').click(function () {
deleteLinkObj = $(this); //for future use
$('#delete-dialog').dialog('open');
return false; // prevents the default behaviour
});
/** Delete dialogs initialisation **/
var i18n_deleteButtons = {};
i18n_deleteButtons[i18n.dialogs_continue] = function () {
$.post(deleteLinkObj[0].href, function (data) { //Post to action
if (data == 'True' || data == 'true') {
deleteLinkObj.closest('tr').hide('fast'); //Hide Row
//(optional) Display Confirmation
}
else {
//(optional) Display Error
}
});
$(this).dialog('close');
};
The code above obviously won’t work because a button is not a link and it has no href attribute..
I would do some type checking. You can use HTML5 data attributes like this:
And in your JS do a check:
This will grab either.