[英]Can not check if input has value or not
我想遍歷id數組並檢查value是否存在,並在下面做一些代碼:
function checkBillingFields() {
var inputIDS = [ "#billing_first_name", "#billing_last_name", "#billing_company", "#billing_phone", "#billing_address_1", "#billing_address_2", "#billing_city", "#billing_city"];
var arrayLength = inputIDS.length;
for (var i = 0; i < arrayLength; i++) {
console.log(inputIDS[i]);
if ($("inputIDS[i]").val().length == 0){
$('.woocommerce-billing-fields__field-wrapper').appendTo('.billing-fields');
$(".billing-heading").css({'margin-top': '100px'});
}else{
$(".expand-billing").css({'display': 'none'});
}
}
}
checkBillingFields();
我收到錯誤消息無法讀取未定義的屬性“長度”。 我使用本地存儲來存儲輸入,並且經過了良好的測試。
在我進行研究並嘗試解決該問題時,任何想法都很有幫助,但沒有任何效果。
將if語句替換為
if ($(inputIDS[i]).val().length == 0)
這將直接將jQuery與數組中的值一起使用,而不是與您的情況中所使用的字符串文字一起使用。
要真正確保您不再遇到任何錯誤,請按照以下步驟進行操作
//First check if element actually exists in DOM
var tmp = $(inputIDS[i]);
if (tmp.length == 1 && tmp.val().length == 0) {
}
就像console.log()
在if
語句中使用一樣
console.log(inputIDS[i]);
if ($(inputIDS[i]).val().length == 0){
inputIDS[i]
//如果i = 0,則返回#billing_first_name
"inputIDS[i]"
將返回"inputIDS[i]"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.