我需要设置一个自定义脚本来跟踪用户点击表单提交字段。这就是我到目前为止所得到的。当用户在表单字段中向下导航时,计数器变量(基数)总计用户沿着路径到达的距离。我想在用户离开页面时通过发送基本变量来发送结果。我正在考虑在 jQuery 中使用 .unload 函数。但是由于某种原因,卸载没有按照我认为的方式响应。有任何想法吗?
var base = 0; //declares a variable of 0. This should refresh when a new user lands on the form page.
function checkPath(fieldNo, path) { //this function should check the current base value of the base variable vs the current fieldNo
if (fieldNo >= path) { //checks to see if base if lower than fieldNo
base = fieldNo; //if true, base is set to current fieldNo
return base;
} else {
return base; //if false, simply returns base.
}
};
$('#order_customer_fields_forename').focus(function () { //when the form box is selected should run checkPath then alert result.
checkPath(1, base);
});
$('#order_customer_fields_surname').focus(function () {
checkPath(2, base);
});
$('#order_customer_fields_postcode').focus(function () {
checkPath(3, base);
});
$('#order_customer_fields_address1').focus(function () {
checkPath(4, base);
});
$('#order_customer_fields_address2').focus(function () {
checkPath(5, base);
});
$(window).unload(function () {
alert(base);
});