我有一个ajax表单,我正在尝试使用Google网站优化工具跟踪。表格会发送电子邮件。
<%= f.email_field :email, :placeholder => "Enter your email address", :size => 40 %>
<%= f.submit '', :class => 'subscriberSubmit', :id => 'subscriber_submit', :onclick => "doGoal(this);return false;" %>
<p>
<input id="subscriber_email" name="subscriber[email]" placeholder="Enter your email address" size="40" type="email">
我的js.erb文件:
$('.loadingImage').fadeOut();
$("#subscriber_submit").removeClass("subscriberSubmitInactive").addClass("subscriberSubmit");
$('#email_form').before("<div id=\"email_sign_up_failure\"><p>Uhm, something's wrong. Mind trying again?</p></div>");
表单工作正常,直到我添加onclick调用来设置GWO信标,它将我发送到未定义的路由。我假设与jQuery冲突,我想我可以从我的js文件而不是在onclick事件中调用该函数,但javascript对我来说是非常新的,我不知道如何调用该函数来自在jQuery中,它本身就是另一段javascript:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['gwo._setAccount', 'UA-1195012']);
function doGoal(that) {
try {
_gaq.push(['gwo._trackPageview', '/136300081/goal']);
setTimeout('document.location = "' + that.href + '"', 100)
}
catch(err){}
}
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End of Google Website Optimizer Tracking Script -->
我在按钮ID上尝试了jQuery的click事件,但它似乎没有用。
<form accept-charset="UTF-8" action="/subscribers" class="new_subscriber" data-remote="true" id="new_subscriber" method="post">
<script>
$(function() {
$( "#subscriber_submit" ).click(function () {
$(this).removeClass("subscriberSubmit").addClass("subscriberSubmitInactive");
$('.loadingImage').fadeIn();
});
});
</script>
<p id="email_form">
<label for="subscriber_email">Email</label><br />
<input id="subscriber_email" name="subscriber[email]" placeholder="Enter your email address" size="40" type="email" />
<input class="subscriberSubmit" id="subscriber_submit" name="commit" type="submit" value="" />
<p>
</form>
</div>
答案 0 :(得分:0)
您可能希望不使用提交按钮,而是使用常规按钮。可能发生的事情是在JS完成执行之前提交表单。您可以做的是在JS点击事件结束时在代码中提交表单。
答案 1 :(得分:0)
我最后只是在_gaq.push(['gwo._trackPageview', '/136300081/goal']);
事件监听器之后将click
放入我的jQuery调用中。