i´ve got a question about closing a form withing a colorbox after submit. I found this question so i now know how to close the form after submit. The problem is that another javascript validates the input fields of my form and there will开发者_运维百科 be an error message (a simple alert()) when some fields are not filled in...i wanne close the clorbox iframe onsubmit only when the validation returns no error..otherwise the user will get the hint that he missed some input fields but the form will close anyway...so how can i check this with jQuery..is there a possiblity to maybe check if an "alert()" is shown on screen...then is could say, when the alertpopup is shown don´t close the colorbox window, otherwise close it because there was no error...does anyone understand what i mean? Again: I cannot change the validation script! Then i could simply add the .colorbox.close();
within this script...
The validation is a simple function which is executed onsubmit...there is a variable "msg" which contains the errormessage within this function..there must be a way to get this "msg" variable when the function executes and to check whether it´s empty or not....?
Can anyone help me with this...it´s a bit difficult to explain for me..
You can probably get the contents of the "msg" element from the iFramed content by referring to it this way:
parent.msg-field-name
What I have done in a similar situation is this:
- Leave a way for the user to cancel the iFrame form. This exit path uses ColorBox's "built-in" close methods
- on Submit, test for error validation. If it passes, use allow the built-in close method to succeed, if it doesn't then don't leave the form.
You can find out exactly how to do form the plugin author's website:
Prevent ColorBox from closing / Change the behavior of $.colorbox.close()
The close method ($.colorbox.close()) can be cached and redefined as to offer some control over what happens when ColorBox is closed. This affects controls that are bind to it (such as the escKey, overlayClose, and the close button) and calling the close method directly.
For example, lets say we open ColorBox to display an html snippet containing a form, and we want to warn the visitor that they are discarding their form if they try to close ColorBox before they submit their data.
var originalClose = $.colorbox.close;
$.colorbox.close = function(){
var response;
if($('#cboxLoadedContent').find('form').length > 0){
response = confirm('Do you want to close this window?');
if(!response){
return; // Do nothing.
}
}
originalClose();
};
$('a#example').colorbox();
精彩评论