简体   繁体   English

单击Event Works,但不单击Window.onunload作为函数

[英]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. 因此,如果我从window.onunload切换并取消注释#driver click事件,则此代码将完全起作用。 I can see it writing to the SQL Database, and I have debugged it prior to making it work on window close. 我可以看到它正在写入SQL数据库,并且在使其在窗口关闭时起作用之前已经对其进行了调试。 Debugged and reliant on a click event, it goes through the full form and submits the information I want. 调试并依赖于click事件,它将通过完整的表单并提交我想要的信息。 I've already checked the PHP code, printing out the correctly formatted strings each way along the path to the SQL database. 我已经检查过PHP代码,并沿着SQL数据库路径打印出格式正确的字符串。

What happens with the window.onunload is that it does synchronous submission, but it is not capturing any form elements. window.onunload发生的事情是它执行同步提交,但是没有捕获任何表单元素。 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 ! 而且您所有的变量都在全局范围内,并且那个选择器太可怕了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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