簡體   English   中英

如何將變量傳入數組JS

[英]How pass the variable in to array JS

問題很簡單。 當我將變量放入數組時,它不起作用。 當我將變量寫入輸入時,它可以工作。 如何將變量傳遞到數組中?

var testW = document.querySelectorAll("input[type='text']");
var testQ = document.getElementsByClassName("deleteObjectbyJS");

var testlength = testW.length;

if(testW[testlength].value == ""){
    testQ[0].style.display = "none";
}

JS中的數組從0SIZE-1索引開始,因此您的代碼在這里是錯誤的:

if(testW[testlength-1].value == ""){
    testQ[0].style.display = "none";
}

我不知道這是否可以完全解決您的問題,但是肯定是錯誤的

testW[testlength]應該是testW[testlength-1]

testlength是列表的長度。 當您嘗試訪問testW [testlength]時,實際上訪問的是最后一個元素之后的不存在的元素,因為元素索引在0到testlength-1之間。

暫無
暫無

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

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