views:

71

answers:

2

I have an Index.aspx page with a button. If you click the button a create overlay pops up. In this overlay lies the form. So the create overlay form page and the Index.aspx are seperated. The whole form is rendered into the Index.aspx using jQuery Overlay plugin from http://flowplayer.org/tools/overlay/index.html (in $(document).ready(function ():

var triggers = $("a[rel]").overlay({
                expose: '#3B5872',
                effect: 'apple',
                closeOnClick: false,
                onBeforeLoad: function () {
                    var wrap = this.getContent().find(".contentWrap");
                    wrap.load(this.getTrigger().attr("href"));
                }
            });

.contentWrap is the div element where the overlay (form) is rendered in.

How can I prevent the form submit via jQuery?

The problem I have is that the form is not there at the $(document).ready(function () function.

When this done I want to send the data from the form via Ajax and update the table on the Index.aspx by also using Ajax with jQuery.

This does not work for now (in document ready function), because the form is not there:

    $("#formCreate").submit(function(event){
        event.preventDefault();
        hijack(this, update_employees, "html");
    });

How can I do this?

Thank you very much in advance!

EDIT Now I tried this in the $(document).ready function, with no success :(

$(document).ready(function () {
            $("a[rel]").overlay({
                mask: '#3B5872',
                effect: 'apple',
                api: true,
                onBeforeLoad: function () {
                    var wrap = this.getOverlay().find(".contentWrap");
                    wrap.load(this.getTrigger().attr("href"));
                },
                onLoad: function () {
                    $("#new_employee").submit(function (event) {
                        event.preventDefault();
                        alert('PREVENT');
                        hijack(this, update_employees, "html");
                    });
                    alert('onLoad');
                }
            });
        });

When I press a button an external page is rendered into a div and the page is popped up as an overlay. The onLoad method is executed AFTER everything is loaded as I understood. And there seems to be the problem. My "new_employee" form is sometimes completly loaded and sometimes not. Because of this at one time I get the alert PREVENT and other times I got no alert. This is in Chrome. In Firefox this does not work at all.

Has anybody any suggestions for me?

+1  A: 

When you create the form you can bind to the form's onSubmit and submit it via ajax and then just return false to stop it from submitting.

For example:

$('.contentWrap form').submit(function() {
        $.ajax({
            type: $(form).attr('method'),
            url: $(form).attr('action'),
            data: $(this).serializeArray(),
            success: function (data) {
               //do something on success
            }
        });
        return false;
}
Matthew Manela
does this really works without having the form actually appended to the div?
Rookian
Where is the form then? If it is being shown then it is in the DOM somewhere. Once you show the form you can run the code above to attach to its submit event
Matthew Manela
the form is on another page (Create.aspx) and will be rendered into the contentWrap div (Index.aspx), when you click the pop-up button.
Rookian
Just run the code then after you load form into the div.
Matthew Manela
yes I tried to do this on the "onLoad" event of the overlay. But it seems to not work correctly :/
Rookian
creating a <script> tag after the form element in the create.aspx solves the problem. But I wonder why the onLoad function of the jQuery tools overlay library does not work correctly :/ In FF it does not work and in Chrome it works only sometimes.
Rookian
+1  A: 

If an element does not exist on the page when the events are bound (when the document is ready in this case) the even can't be bound to it. It needs to be bound after the element is created. So, after you run the code to open the overlay, you then need to run:

$(submitButton).submit(function(event) {event.preventDefault();});

So, I can't see your page, but it would be something like:

$(overlay).open();
$(submitButton).submit(function(event) {event.preventDefault();});

Just having it in the .ready will not work.

the problem I have is where shall I write the preventDefault function? the form has no load event. I tried it at the overlay event "onLoad" (http://flowplayer.org/tools/overlay/index.html), but it does not work. What is the best place for my situation to place the preventing code in?
Rookian
Again you haven't show us the code where you are loading the form, but basically you said "the form is on another page (Create.aspx) and will be rendered into the contentWrap div (Index.aspx), when you click the pop-up button." So, that is where the code needs to go. You need something like:$(pop-up button).click(function(event) {$(pop-up).open();$("#formCreate").submit(function(event){ event.preventDefault(); hijack(this, update_employees, "html");});});
look at my edited question please ...
Rookian
"The onLoad method is executed AFTER everything is loaded as I understood" true, but "everything" in this case is "everything that is currently being loaded" if you have an AJAX call that happens after the page has loaded (that is usually when AJAX calls happen) the onLoad happens BEFORE that AJAX call. Everything that was to be loaded on the page was loaded, the onLoad was called, then you did an AJAX call. The code you updated with does NOT show where you are AJAXily loading up the form, from Create.aspx, in fact I see no reference to Create.aspx anywhere in your code.