我在以下代码中有一些条件变量。例如,如果var shipping ==“ true”,则存在shippingAddress1,shippingAddress2等。但是,如果shipping ==“ false”,则这些变量是未定义的,并且留为空白。我的问题是,当我尝试发布时,由于某些var未定义,所以出现js错误。有什么办法可以通过空格发送这些吗?
$.post( 'reserveAPickupAppointmentCreatePickup.php', {
'location': appointmentLocation,
'appointmentAddress1': appointmentAddress1,
'appointmentAddress2': appointmentAddress2,
'serviceType': serviceType,
'shipping': shipping,
'shippingAddress1': shippingAddress1,
'shippingAddress2': shippingAddress2,
'shippingCity': shippingCity,
'shippingState': shippingState,
'shippingZip': shippingZip,
'startTime': startTime,
'endTime': endTime,
'date': date,
'estimatedSize': appointmentSize,
'highValueItems': highValueItems,
'insuredItems': insuredItems,
'tools': tools,
'presentAtAppointment': presentAtAppointment,
'contactName': contactName,
'contactPhone': contactPhone,
'contactEmail': contactEmail,
'itemList': itemList,
'studentPhone': studentPhone
},
最佳答案
为您使用的变量设置合理的默认值。如果是字符串,则默认为空字符串。通常,如果某些时候没有定义变量,请不要使用。