javascript 通过javascript向表单添加onsubmit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13849659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding onsubmit to a form via javascript
提问by alex
How would you go about inserting an OnSubmit attribute to a form via Javascript only?
您将如何仅通过 Javascript 将 OnSubmit 属性插入表单?
I'm pretty new to javascript so if you're able to provide detailed example code, that would be most helpful!
我对 javascript 很陌生,所以如果您能够提供详细的示例代码,那将是最有帮助的!
Here's the situation:I'm using a hosted signup page through Chargify (a payments platform) in order to process credit cards for my app, and then send the user back to my own thank you/confirmation page.
情况是这样的:我通过 Chargify(一个支付平台)使用托管注册页面来处理我的应用程序的信用卡,然后将用户发送回我自己的感谢/确认页面。
Tracking the entire funnel through google analytics is proving quite elusive due to changing domains (my domain -> Chargify.com -> my domain), since the credit card page is hosted by Chargify on their own domain.
由于域的变化(我的域 -> Chargify.com -> 我的域),通过谷歌分析跟踪整个漏斗被证明是非常难以捉摸的,因为信用卡页面由 Chargify 在他们自己的域上托管。
I'm getting close:I've been able to get cross-domain tracking working (chargify.com page gets logged in Google Analytics), and can link from my domain to chargify by adding the following onclick attribute to my signup link:
我越来越接近了:我已经能够进行跨域跟踪(chargify.com 页面已登录 Google Analytics),并且可以通过将以下 onclick 属性添加到我的注册链接来从我的域链接到 chargify:
onclick="_gaq.push(['_link', 'http://my-domain.chargify.com/subscriptions/new']); return false;"
However, I cannot do the same thing on the way back (Chargify -> Confirmation page) because I do not have access to the Chargify hosted payment page code, and because the user is taken to my confirmation page via a form submission, not a normal link.
但是,我不能在返回的路上做同样的事情(Chargify -> 确认页面),因为我无权访问 Chargify 托管的付款页面代码,并且因为用户是通过表单提交而不是表单提交到我的确认页面正常链接。
Partial Solutions (need your help to finish this up):Chargify allows several options for their hosted pages, one of them being to add custom javascript that gets inserted right before the </body>
tag in a <script>
tag.
部分解决方案(需要您的帮助来完成):Chargify 允许为其托管页面提供多个选项,其中之一是添加自定义 javascript,该 javascript 将在</body>
标签中的 <script>
标签之前插入 。
I found some resources in the Google Analytics documentation on how to link pages, and adding the following to the Chargify form tag might work: onsubmit="_gaq.push(['_linkByPost', this]);"
(source: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost)
我在 Google Analytics 文档中找到了一些关于如何链接页面的资源,将以下内容添加到 Chargify 表单标签可能会起作用:( onsubmit="_gaq.push(['_linkByPost', this]);"
来源:https: //developers.google.com/analytics/devguides/collection/gajs/methods/ gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost)
The form tag does not currently have an onsubmit attribute, it's just this: <form action="/my_product/subscriptions" class="new_submission" id="hosted_payment_form" method="post">
表单标签目前没有 onsubmit 属性,它只是这样: <form action="/my_product/subscriptions" class="new_submission" id="hosted_payment_form" method="post">
Is there a way to use Javascript to simply append this attribute to the form tag?If you'd be able to provide a detailed example of what code I should insert inside of the <script>
tag, that would be extremely appreciated.
有没有办法使用 Javascript 简单地将此属性附加到表单标签?如果您能够提供我应该在<script>
标签内插入什么代码的详细示例 ,那将不胜感激。
回答by Weave
window.onload = function() {
var form = document.getElementById('hosted_payment_form');
form.onsubmit = function() {
_gaq.push(['_linkByPost', this]);
}
}
I believe the above example is similar to what you need. We use document.getElementById to grab a reference to your form. Then set the onsubmit property to the code you want executed before the form is submitted. Remember to put this inside the window onload event if this JavaScript is executed before the page is rendered to ensure the form is built.
我相信上面的例子与你需要的相似。我们使用 document.getElementById 来获取对表单的引用。然后将 onsubmit 属性设置为您要在提交表单之前执行的代码。如果在呈现页面之前执行此 JavaScript 以确保构建表单,请记住将其放在窗口 onload 事件中。