简体   繁体   中英

Click Event Works, but not Window.onunload as a function

So, this code fully works if I switch from the window.onunload and uncomment the #driver click event. I can see it writing to the SQL Database, and I have debugged it prior to making it work on window close. Debugged and reliant on a click event, it goes through the full form and submits the information I want. I've already checked the PHP code, printing out the correctly formatted strings each way along the path to the SQL database.

What happens with the window.onunload is that it does synchronous submission, but it is not capturing any form elements. All it submits is the defined cart data.

The question is, why is the behavior so different when switching from one event trigger to another?

var formData = new Array();
var orderSubTotal ='46.15';
var orderTotal  ='46.15';
var numOfItems ='2';
var items =new Array('item1','item2');
var ids =new Array('id1','id2');
var codes =new Array('code1','code2');
var qtys =new Array('1','1');
var price =new Array('44.95','1.2');
var orderTax ='0';
var orderShipping ='0';
var appliedPromoIdList ='';
var coupon ='';
var storeId ='storeid';
var activeShipPromotionCount ='';
var itemImages  =new Array('image1','image2');

$(document).ready(function() {

//$("#driver").click(function() {
    function submitform(formData) {
    var formData = $("#testform :input[id!='card-type'][id!='paymentSelection_0']"+
    "[id!='ccSelectedRadio'][id!='card-number'][id!='card-exp-month'][id!='card-exp-year'][id!='card-cvv'][id!='billing-first-name']"+
    "[id!='billing-last-name'][id!='billing-company'][id!='billing-address1'][id!='billing-address2'][id!='billing-city']"+
    "[id!='billing-state'][id!='billing-zip'][id!='billing-phone'][id!='billing-country'][id!='useShippingRadio'][id!='useBillingRadio']"+
    "[id!='ppSelectedRadio'][name!='miscDS.shopperEmailAddress_ymixval'][name!='miscDS.shopperEmailAddress_ymixlabel']"+
    "[name!='miscDS.shopperEmailAddress_secname'][name!='paymentSelectionDS.paymentSelection_ROW0_paymentPPSelected']").serializeArray();
    var date=new Date();

    $.ajax(
        {
            url: 'jquery/process.php',
            data: {
            mydata: formData,
            orderSubTotal: orderSubTotal,
            orderTotal: orderTotal,
            numOfItems: numOfItems,
            items: items,
            ids: ids,
            codes: codes,
            qtys: qtys,
            price: price,
            orderTax: orderTax,
            orderShipping: orderShipping,
            appliedPromoIdList: appliedPromoIdList,
            coupon: coupon,
            storeId: storeId,
            activeShipPromotionCount: activeShipPromotionCount,
            itemImages: itemImages,
            date: date
            },
            async: false
        });
        return formData;
};
window.onunload(submitform());
});

Adding the parenthesis executes the function immediately:

window.onunload(submitform());

you should just reference it:

window.onunload = submitform;

And all your variables is in the global scope, and that selector is horrendous !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM