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

时间:2013-11-26 22:34:45

标签: javascript php jquery

因此,如果我从window.onunload切换并取消注释#driver click事件,则此代码完全有效。我可以看到它写入SQL数据库,我已经调试它之前使它在窗口关闭。调试并依赖于单击事件,它将通过完整的表单并提交我想要的信息。我已经检查过PHP代码,在SQL数据库路径的每一个方向上打印出格式正确的字符串。

window.onunload会发生什么,它会执行同步提交,但它不会捕获任何表单元素。它提交的只是定义的购物车数据。

问题是,当从一个事件触发器切换到另一个事件触发器时,为什么行为如此不同?

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());
});

1 个答案:

答案 0 :(得分:3)

添加括号会立即执行该功能:

window.onunload(submitform());

你应该参考它:

window.onunload = submitform;

并且所有变量都在全局范围内,并且该选择器非常可怕!