我正在使用 JavaScript 来验证表单字段。如果它通过了,我想向谷歌分析发送一个事件。但是,它不会将事件发送到 GA。在我的表格中,我有:
<form role="form" name="submit" method="post" onSubmit="return validateForm();">
我的验证码是:
function validateForm() {
// validate the form and update the section if there is an error
var spamCheck = document.submit.inputSpamCheck.value;
if( document.submit.inputName.value == '' ){
document.submit.inputName.focus();
$( "#name-section" ).addClass( "has-error" );
return false;
}
else if( document.submit.inputEmail.value == '' ){
document.submit.inputEmail.focus();
$( "#email-section" ).addClass( "has-error" );
return false;
}
else if( document.submit.inputSpam.value != spamCheck ){
document.submit.inputSpam.focus();
$( "#spam-section" ).addClass( "has-error" );
return false;
}
else{
_gaq.push(['_trackEvent', 'Form', 'Submitted', 'successful', 5]);
return true;
}
}