0

フォーム送信フィールドでのユーザーのクリックスルーを追跡するためのカスタム スクリプトを設定する必要があります。これは私がこれまでに得たものです。ユーザーがフォーム フィールドを下に移動すると、カウンター変数 (ベース) は、ユーザーがパスに沿って到達した距離を合計します。基本変数を送信して、ユーザーがページを離れたときに結果を送信したいと考えています。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);
});
4

1 に答える 1

0

必要な効果に対してイベントのunload発生が遅すぎます。onbeforeunloadいずれかのバニラ Javascript を使用してイベントを使用してみてください。

window.onbeforeunload = function (e) {
    // Your code here
};

または jQuery:

$(window).bind('beforeunload', function (e) {
    // Your code here
});

いずれにせよ、これはあなたが達成しようとしていることに対する理想的な解決策ではないことに注意してください。このイベントは、ブラウザー間で不均等に実装されます。その実装において、Chrome は最も制限的であり、IE は最も寛容であると思われます。

別の方法として、ユーザーがフィールドに入力するたびに XHR によってデータをサーバーに送信することもできます。

于 2012-12-12T12:39:06.370 に答える