我正在使用Jquery Validation 1.9.0插件来验证我的表单,然后再提交。
我在表格中的提交按钮上有谷歌事件跟踪代码。onClick
属性,在表单验证完成和表单实际提交之前,我不想启动该功能,以便在谷歌跟踪的数据上有更好的准确性。
<button class="btn btn-primary" type="submit" onClick="ga('send', 'event', 'Form Submit', 'Button Click', 'A visitor has filled out the form.');">Submit</button>
有什么干净的方法可以防止在表单中的函数 onClick
属性,一旦表单验证完成,表单就会提交,但就在提交之前,应该启动 ga
事件跟踪代码。
使用 提交处理程序 的选择。$.validate
调用。 它只有在表单有效时才会被触发。
$(form).validate({
//your usual stuff here
submitHandler: function(){
ga('send', 'event', 'Form Submit', 'Button Click', 'A visitor has filled out the form.');
}
});
你应该在验证完表单后,在表单提交事件上触发分析事件。
在你的表单上添加一个onsubmit事件。
<form onsubmit="submitMyForm(this);return false;">...</form>
最好的办法是先在一个方法中进行。 我在你的表单中设置了一个onsubmit的函数,就像这样。
funtion submitMyForm(form)
{
if ($(form).valid())
{
ga('send', 'event', 'Form Submit', 'Button Click', 'A visitor has filled out the form.');
$(form).submit();
}
}