簡體   English   中英

單擊Event Works,但不單擊Window.onunload作為函數

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

因此,如果我從window.onunload切換並取消注釋#driver click事件,則此代碼將完全起作用。 我可以看到它正在寫入SQL數據庫,並且在使其在窗口關閉時起作用之前已經對其進行了調試。 調試並依賴於click事件,它將通過完整的表單並提交我想要的信息。 我已經檢查過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());
});

添加括號會立即執行該函數:

window.onunload(submitform());

您應該只引用它:

window.onunload = submitform;

而且您所有的變量都在全局范圍內,並且那個選擇器太可怕了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM