jQuery / JS设置全局变量但是传递给AJAX自定义函数

时间:2021-06-18 20:36:31

I'm trying to set some global vars but a little stuck on how to then fire the custom function which does a AJAX request with the data, I collect email in one function and other events and also firstname and lastname but I'm now stuck.

我正在尝试设置一些全局变量,但稍微停留在如何然后触发与数据执行AJAX请求的自定义函数,我在一个函数和其他事件中收集电子邮件以及firstname和lastname但我现在卡住了。

I've not called fireCheckoutAC anywhere as this is what I'm stuck with. I'm new to jQuery and JS but think I've got as far as I can.

我没有在任何地方调用fireCheckoutAC,因为这是我坚持的。我是jQuery和JS的新手,但我想我已经尽力了。

// set global variables
var checkoutEmail = "";
var checkoutFirstName = "";
var checkoutLastName = "";

$(document).ready(function() {

    function fireCheckoutAC(checkoutEmail, checkoutFirstName, checkoutLastName) {
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': checkoutEmail,
                'firstname': checkoutFirstName,
                'lastname': checkoutLastName
            },
            caching: true
        });
    }
    // function to check email field, validate and save to ac for this customer session
    function checkIt(field) {
        field = $(field);
        var email = field.val();
        var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
        var emailInputId = field.attr('id');
        if ($("." + emailInputId + "_error_message").length > 0) {
            $("." + emailInputId + "_error_message").remove();
        }
        //console.log($(emailInputId+"_error_message"));
        if (validEmail(email)) {
            //alert('valid email');
            checkoutEmail = email;
            field.removeClass('cm-failed-field');
            field.prev().removeClass('cm-failed-label');
            field.next("span").remove();
        } else {
            field.addClass('cm-failed-field');
            field.prev().addClass('cm-failed-label');
            field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
        }
    }

    // lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
    var field = $('#onestepcheckout .ty-billing-email input')[0];
    if ($(field).length > 0) {
        if (field.value) {
            checkIt(field);
        }
    }

    // check email thats inputted and save to ac session for this customer, or if email changed to update
    $('#onestepcheckout .ty-billing-email input').blur(function() {
        checkIt(this);
    });

    // if first name entered lets grab it and add to the ac session for the customer
    var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
    var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

    $(firstname_sel+','+lastname_sel).blur(function() {
        checkoutFirstName = $(firstname_sel).val();
        checkoutLastName = $(lastname_sel).val();
    });

    // lets grab the first name and last name if already in input
    var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
    var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
    if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
        if (firstname_sel_pre.value || lastname_sel_pre.value) {
            checkoutFirstName = $(firstname_sel_pre).val();
            checkoutLastName = $(firstname_sel_pre).val();
        }
    }

});

1 个解决方案

#1


0  

Managed to work it out, here is the solution for anyone else wanting to know... just had to call the ajax function which handled the global vars

管理工作,这里是其他任何想要知道的解决方案......只需要调用处理全局变量的ajax函数

/* grab completed email when enetred into checkout and add to abandoned cart 

for that session */
function validEmail(v) {
    var r = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return (v.match(r) == null) ? false : true;
}

// set global variables
var checkoutEmail = "";
var checkoutFirstName = "";
var checkoutLastName = "";

$(document).ready(function() {

    function fireCheckoutAC() {
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': checkoutEmail,
                'firstname': checkoutFirstName,
                'lastname': checkoutLastName
            },
            caching: true
        });
    }
    // function to check email field, validate and save to ac for this customer session
    function checkIt(field) {
        field = $(field);
        var email = field.val();
        var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
        var emailInputId = field.attr('id');
        if ($("." + emailInputId + "_error_message").length > 0) {
            $("." + emailInputId + "_error_message").remove();
        }
        //console.log($(emailInputId+"_error_message"));
        if (validEmail(email)) {
            //alert('valid email');
            checkoutEmail = email;
            fireCheckoutAC();
            field.removeClass('cm-failed-field');
            field.prev().removeClass('cm-failed-label');
            field.next("span").remove();
        } else {
            field.addClass('cm-failed-field');
            field.prev().addClass('cm-failed-label');
            field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
        }
    }

    // lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
    var field = $('#onestepcheckout .ty-billing-email input')[0];
    if ($(field).length > 0) {
        if (field.value) {
            checkIt(field);
        }
    }

    // check email thats inputted and save to ac session for this customer, or if email changed to update
    $('#onestepcheckout .ty-billing-email input').blur(function() {
        checkIt(this);
    });

    // if first name entered lets grab it and add to the ac session for the customer
    var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
    var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

    $(firstname_sel+','+lastname_sel).blur(function() {
        checkoutFirstName = $(firstname_sel).val();
        checkoutLastName = $(lastname_sel).val();
        fireCheckoutAC();
    });

    // lets grab the first name and last name if already in input
    var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
    var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
    if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
        if (firstname_sel_pre.value || lastname_sel_pre.value) {
            checkoutFirstName = $(firstname_sel_pre).val();
            checkoutLastName = $(firstname_sel_pre).val();
            fireCheckoutAC();
        }
    }

});

#1


0  

Managed to work it out, here is the solution for anyone else wanting to know... just had to call the ajax function which handled the global vars

管理工作,这里是其他任何想要知道的解决方案......只需要调用处理全局变量的ajax函数

/* grab completed email when enetred into checkout and add to abandoned cart 

for that session */
function validEmail(v) {
    var r = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return (v.match(r) == null) ? false : true;
}

// set global variables
var checkoutEmail = "";
var checkoutFirstName = "";
var checkoutLastName = "";

$(document).ready(function() {

    function fireCheckoutAC() {
        $.ceAjax('request', fn_url('ac.email'), {
            method: 'post',
            data: {
                'email': checkoutEmail,
                'firstname': checkoutFirstName,
                'lastname': checkoutLastName
            },
            caching: true
        });
    }
    // function to check email field, validate and save to ac for this customer session
    function checkIt(field) {
        field = $(field);
        var email = field.val();
        var emailError = "<p>The email address in the <b>E-mail</b> field is invalid.</p>";
        var emailInputId = field.attr('id');
        if ($("." + emailInputId + "_error_message").length > 0) {
            $("." + emailInputId + "_error_message").remove();
        }
        //console.log($(emailInputId+"_error_message"));
        if (validEmail(email)) {
            //alert('valid email');
            checkoutEmail = email;
            fireCheckoutAC();
            field.removeClass('cm-failed-field');
            field.prev().removeClass('cm-failed-label');
            field.next("span").remove();
        } else {
            field.addClass('cm-failed-field');
            field.prev().addClass('cm-failed-label');
            field.after("<span class='" + emailInputId + "_error_message help-inline' ><p>" + emailError + "</p></span>");
        }
    }

    // lets check if the email input was already populated, such as browser auto fill etc.. if so use that and save
    var field = $('#onestepcheckout .ty-billing-email input')[0];
    if ($(field).length > 0) {
        if (field.value) {
            checkIt(field);
        }
    }

    // check email thats inputted and save to ac session for this customer, or if email changed to update
    $('#onestepcheckout .ty-billing-email input').blur(function() {
        checkIt(this);
    });

    // if first name entered lets grab it and add to the ac session for the customer
    var firstname_sel = '#onestepcheckout .ty-billing-first-name input';
    var lastname_sel = '#onestepcheckout .ty-billing-last-name input';

    $(firstname_sel+','+lastname_sel).blur(function() {
        checkoutFirstName = $(firstname_sel).val();
        checkoutLastName = $(lastname_sel).val();
        fireCheckoutAC();
    });

    // lets grab the first name and last name if already in input
    var firstname_sel_pre = $('#onestepcheckout .ty-billing-first-name input')[0];
    var lastname_sel_pre = $('#onestepcheckout .ty-billing-last-name input')[0];
    if ($(firstname_sel_pre).length > 0 || $(lastname_sel_pre).length > 0) {
        if (firstname_sel_pre.value || lastname_sel_pre.value) {
            checkoutFirstName = $(firstname_sel_pre).val();
            checkoutLastName = $(firstname_sel_pre).val();
            fireCheckoutAC();
        }
    }

});