因此,如果我从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;
而且您所有的变量都在全局范围内,并且那个选择器太可怕了!