Javascript 使用Jquery设置表单跟踪
我需要设置一个自定义脚本来跟踪用户在表单提交字段上的点击。这就是我到目前为止得到的。当用户在表单字段中向下导航时,计数器变量(base)总计用户已到达的路径距离。我想在用户离开页面时通过发送基本变量发送结果。我正在考虑在jQuery中使用.unload函数。但是由于某些原因,卸载没有以我认为应该的方式响应。有什么想法吗Javascript 使用Jquery设置表单跟踪,javascript,jquery,forms,Javascript,Jquery,Forms,我需要设置一个自定义脚本来跟踪用户在表单提交字段上的点击。这就是我到目前为止得到的。当用户在表单字段中向下导航时,计数器变量(base)总计用户已到达的路径距离。我想在用户离开页面时通过发送基本变量发送结果。我正在考虑在jQuery中使用.unload函数。但是由于某些原因,卸载没有以我认为应该的方式响应。有什么想法吗 var base = 0; //declares a variable of 0. This should refresh when a new user lands on th
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);
});
unload
事件触发太晚,无法达到所需的效果。您应该尝试使用以下任意一种Javascript使用onbeforeunload
事件:
window.onbeforeunload = function (e) {
// Your code here
};
或jQuery:
$(window).bind('beforeunload', function (e) {
// Your code here
});
无论哪种方式,您都应该意识到,这并不是您试图实现的理想解决方案。此事件在浏览器中的实现不均衡。Chrome在它的实现中似乎是限制性最强的
您可能希望采取的另一个方向是,每当用户填写字段时,通过XHR将数据发送到服务器