简体   繁体   中英

Catching form submit result

I have a page which calls an external library, this library adds a payment form:

<form name="multipay_form" onsubmit="private_payment_send(); return false;">
     ....
</form>

I can not change any code here. I need to call a function after form is submitted. What I have done is:

jQuery('form[name="multipay_form"]').on('submit', function() {
    alert("myfunction");
});

Which works ok, but there is one exception, the method "private_payment_send()", does form validation, I need to know if their function returned true or false, to be able to trigger my function or not.

Is this possible?. I want to avoid doing the validation again on my end, since if they add new field or any new rule I would have to do the same on my code, which is not optimal. Any ideas?

Is there a way to unattach the function from the form through javascript?, in that way I can call private_payment_send() from my function

<form name="multipay_form" onsubmit="private_payment_send(); return false;">
     <button type="submit">test</button>
</form>

 document.getElementsByName("multipay_form")[0].setAttribute('onsubmit','');

This will make it so the onsubmit is removed from the form without touching the HTML

Try to use done() function in Jquery

.done(function( n ) {
    $( "p" ).append( n + " we're done." );
  });

Following is Jquery documentation

https://api.jquery.com/deferred.done/

You can trigger your function only when the called function, here "private_payment_send" has returned true. This can be done like this

<form name="multipay_form" onsubmit="if (private_payment_send()) { alert("myFunction") }; return false;">
    ...
</form>

If you only want to use the jQuery part, you can completely remove the onSubmit attribute and only assign the submit handler using jQuery

jQuery('form[name="multipay_form"]').on('submit', function() {
    if (private_payment_send())
        alert("myfunction");

    return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM