我使用Jquery Validation 1.9.0插件在提交表单之前验证我的表单。
我在onClick属性中的表单提交按钮上有Google事件跟踪代码,我不想触发,直到表单验证完成,并且表单实际上提交了对Google跟踪的数据更准确的信息。
<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事件跟踪代码。
答案 0 :(得分:2)
使用$.validate
电话的submitHandler选项。它仅在表单有效时触发:
$(form).validate({
//your usual stuff here
submitHandler: function(){
ga('send', 'event', 'Form Submit', 'Button Click', 'A visitor has filled out the form.');
}
});
答案 1 :(得分:0)
在验证表单后,您应该在表单提交事件上触发分析事件:
在表单上添加一个onsubmit事件:
<form onsubmit="submitMyForm(this);return false;">...</form>
最好的方法是首先在方法中执行此操作。我在您的表单中有一个函数onsubmit liek this:
funtion submitMyForm(form)
{
if ($(form).valid())
{
ga('send', 'event', 'Form Submit', 'Button Click', 'A visitor has filled out the form.');
$(form).submit();
}
}