通过JavaScript将onsubmit添加到表单 [英] Adding onsubmit to a form via javascript

查看:122
本文介绍了通过JavaScript将onsubmit添加到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何仅通过Java脚本将OnSubmit属性插入表单?

我对javascript很陌生,所以如果您可以提供详细的示例代码,这将是最有用的!

I'm pretty new to javascript so if you're able to provide detailed example code, that would be most helpful!

在这种情况下:我正在使用托管的注册页面通过Chargify(付款平台)处理我的应用程序的信用卡,然后将用户送回我自己的谢谢/确认页面。

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.com->我的域名),因此通过Google Analytics(分析)跟踪整个渠道非常困难,因为信​​用卡页面是由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页面已登录Google Analytics(分析) ),并可以通过将以下onclick属性添加到我的注册链接中来从我的域进行链接:

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:

onclick="_gaq.push(['_link', 'http://my-domain.chargify.com/subscriptions/new']); return false;"

但是,我不能在返回途中做同样的事情(收费->确认页面),因为我没有访问Chargify托管的付款页面代码的权限,并且由于用户是通过表单提交而不是常规链接进入我的确认页面的。

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允许为其托管页面提供多个选项,其中之一是添加自定义javascript,该javascript会在之前插入; / body> 标签放在< script> 标签中。

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.

我在Google Analytics(分析)文档中找到了一些有关如何链接页面的资源,并且将以下内容添加到Chargify表单标记中可能有效: onsubmit = _gaq.push([[_ linkByPost',this]);
(来源: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost

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)

form标记当前没有onsubmit属性,仅此而已:< form action = / my_product / subscriptions class = new_submission id = hosted_pa​​yment_form method = post>

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">

是否有使用Javascript的方法如果您能够提供一个详细示例,说明我应该在< script> 标记内插入哪些代码,这将是非常感谢。

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.

推荐答案

window.onload = function() {
    var form = document.getElementById('hosted_payment_form');
    form.onsubmit = function() {
        _gaq.push(['_linkByPost', this]);
    }
}

我相信以上示例与您所需要的相似。我们使用document.getElementById来获取对您表单的引用。然后将onsubmit属性设置为提交表单之前要执行的代码。请记住,如果在呈现页面之前执行此JavaScript以确保构建表单,则将其放在window onload事件中。

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.

这篇关于通过JavaScript将onsubmit添加到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆