首页 > 解决方案 > queryselected/getElementByClass 的值返回值为空

问题描述

当我尝试从另一个模块中查询选择元素时,它总是返回空字符串?应该怎么做才能解决这个问题?该元素存在是因为当我控制台记录它时,该元素就在那里,即使我在 UI 中填写了输入,该值也总是返回空,只是当我按下提交时它返回空。

此外,如果我将代码移动到导出函数的范围内,它就可以工作

下面的代码在函数 footerSubmitSection() 内,该函数被导出到另一个名为 addNewOrderFormMain.js 的模块,其中所有组件都被附加到主容器并导出到 index.js

console.log 返回

// order ID
        db.collection('UserDatabase').doc(currentUserID).get().then((doc) => {

                function dateForOrder(){
                    let today = new Date();
                    let dd = String(today.getDate()).padStart(2, '0');
                    let mm = String(today.getMonth() + 1).padStart(2, '0');
                    let yyyy = today.getFullYear();
                    today = mm+dd+yyyy;
                    return today
                } 

                let todayOrderNum = db.collection('OrderDatabase').where('OrderDate', '==', `${orderDate}`)
                    .get()
                    .then((snapshot) => {
                        length = snapshot.size + 1;
                        let salesRepID = doc.data().SaleID
                        let salesRepName = doc.data().Name
                        let orderID = salesRepID + dateForOrder()+'-'+ length + "-"+ editversion
                 
                        console.log(deliveryMethodInput)
                        if (deliveryMethodInput=== 'Pick Up'){
                            const pickupLocationInput = document.getElementsByClassName('orderDetailSectionMidPickupLocationSelect')[0].value;
                            let pickupLocation = pickupLocationInput;
                             db.collection('OrderDatabase').doc(orderID).set({
                                    PickupLocation:pickupLocation,
                                    ShippingAddress:"",
                                    BusinessName:businessName,
                                    CustomerType:customerType,
                                    BillingAddress:billingAddress,
                                    CustomerPhoneNumber:customerPhoneNum,
                                    DeliveryMethod:deliveryMethod,
                                    ShipOrPickDate:shipOrPickDate,
                                    PaymentMethod:paymentMethod,
                                    Notes:notes,
                                    OrderDate:orderDate,
                                    OrderStatus:orderStatus,
                                    OrderID:orderID,
                                    salesRepID:salesRepID,
                                    salesRepName:salesRepName,
                            })

                            db.collection('CustomerDatabase').get().then((snapshot) => {
                                snapshot.forEach((doc) => {
                                    if(businessNameValue === doc.data().BusinessName){
                                        let customerID = doc.data().CustomerID;
                                        db.collection('OrderDatabase').doc(orderID).update({
                                            CustomerID:customerID,
                                        })
                                    }
                                })
                            })



                            let subtotalArray=[];  /////////////this is the block im having issue with
                            let sum = 0
                            const basket = document.getElementsByClassName('orderProductSectionMidItemContainer')
                            Array.from(basket).forEach((basketItem) => {
                                const basketItemName = basketItem.getElementsByClassName('orderProductSectionMidDisplayItemNameInput')[0];
                                const basketItemPrice = basketItem.getElementsByClassName('orderProductSectionMidDisplayPriceInput')[0];
                                const basketItemQuantity = basketItem.getElementsByClassName('orderProductSectionMidDisplayQuantitiyInput')[0];
                                const basketItemDiscount = basketItem.getElementsByClassName('orderProductSectionMidDisplayDiscountInput')[0];
                                const basketItemSubtotal = basketItem.getElementsByClassName('orderProductSectionMidDisplaySubtotal')[0];
                                subtotalArray.push(basketItemSubtotal.value);
                    
                                console.log(basketItemName.value)
                                console.log(basketItemName.value)
                                console.log(basketItemPrice.value)
                                console.log(basketItemQuantity.value)
                
                                db.collection('OrderDatabase').doc(orderID).collection('Basket').doc('testing').set({
                                    ItemName:basketItemName.value,
                                    ItemPrice:basketItemPrice.value,
                                    ItemQuantity:basketItemQuantity.value,
                                    ItemDiscount:basketItemDiscount.value,
                                    ItemSubtotal:basketItemSubtotal.value ,
                                })
                                console.log(subtotalArray)
                            })
                
                            console.log(basket)
                            for(let i = 0; i < subtotalArray.length; i++){
                                sum += parseFloat(subtotalArray[i]);
                            }/////////////block ends here

                        }else if(deliveryMethodInput === 'Ship'){
                             db.collection('OrderDatabase').doc(orderID).set({
                                    ShippingAddress:shippingAddress,
                                    PickupLocation:"",
                                    BusinessName:businessName,
                                    CustomerType:customerType,
                                    BillingAddress:billingAddress,
                                    CustomerPhoneNumber:customerPhoneNum,
                                    DeliveryMethod:deliveryMethod,
                                    ShipOrPickDate:shipOrPickDate,
                                    PaymentMethod:paymentMethod,
                                    Notes:notes,
                                    OrderDate:orderDate,
                                    OrderStatus:orderStatus,
                                    OrderID:orderID,
                                    salesRepID:salesRepID,
                                    salesRepName:salesRepName,
                                })
                            db.collection('CustomerDatabase').get().then((snapshot) => {
                                snapshot.forEach((doc) => {
                                    if(businessNameValue === doc.data().BusinessName){
                                        let customerID = doc.data().CustomerID;
                                        db.collection('OrderDatabase').doc(orderID).update({
                                            CustomerID:customerID,
                                        })
                                    }
                                })
                            })

                        }
                
                    })

        

            })

标签: javascript

解决方案


推荐阅读