[英]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. 可以,但是有一个例外,方法“ private_payment_send()”可以进行表单验证,我需要知道其函数返回的是true还是false,才能触发我的函数。
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 有没有办法通过javascript从表单中取消附加功能?通过这种方式,我可以从我的功能中调用private_payment_send()
<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 这样就可以将onsubmit从表单中删除,而无需触摸HTML
Try to use done() function in Jquery 尝试在Jquery中使用done()函数
.done(function( n ) {
$( "p" ).append( n + " we're done." );
});
Following is Jquery documentation 以下是Jquery文档
https://api.jquery.com/deferred.done/ https://api.jquery.com/deferred.done/
You can trigger your function only when the called function, here "private_payment_send" has returned true. 仅当被调用的函数(此处的“ private_payment_send”)返回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部分,则可以完全删除onSubmit属性,仅使用jQuery分配提交处理程序。
jQuery('form[name="multipay_form"]').on('submit', function() {
if (private_payment_send())
alert("myfunction");
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.