Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

javascript - Iterating over a nodelist, getting the length of a value of a node item

I am trying to dynamically grab ZIPcodes and validate them when the length is 5.

I used querySelectorAll to grab the Zipcode fields on the page, as well as a few other fields I will use after validating.

I iterate over the nodelist and pass it to another function, where the eventlistener kicks off if the value is the correct length.

function GetZipCodeDetails() {

            var zipId = document.querySelectorAll("[id*='ZipCode']");
            var countyId = document.querySelectorAll("[id*='CountyId']");
            var stateId = document.querySelectorAll("[id*='StateId']");
            var phoneId = document.querySelectorAll("[id*='PhoneNumber']");

            for (var i = 0; i < zipId.length; i++) {
                if (zipId[i].length = 5)
                AssortedZipCodeFunctions(zipId[i], countyId[i], stateId[i], phoneId[i]);
            }
        }

        function AssortedZipCodeFunctions(zipId, countyId, stateId, phoneId) {
            //Runs auto-county/state function only when zipcode field is completed
            document.addEventListener("keyup", (e) => {
                if (zipId.value.length == 5) {
                    GetCountyAndStateFromIds(zipId, countyId, stateId, phoneId);
                } });
        }

The code works perfectly for me as it is listed above; I am just trying to move the second function into the first function, but I can't figure out how. I am just stuck on how come I can't do the following:

function GetZipCodeDetails() {

            var zipId = document.querySelectorAll("[id*='ZipCode']");
            var countyId = document.querySelectorAll("[id*='CountyId']");
            var stateId = document.querySelectorAll("[id*='StateId']");
            var phoneId = document.querySelectorAll("[id*='PhoneNumber']");

            for (var i = 0; i < zipId.length; i++) {
                document.addEventListener("keyup", (e) => {
                    if (zipId[i].value.length == 5) {
                        GetCountyAndStateFromIds(zipId[i], countyId[i], stateId[i], phoneId[i]);
                    }
                });
            }
        }

The above gives: "TypeError: Cannot read property 'value' of undefined at HTMLDocument."

I have figured out that the for loop is calling the second function, instead of waiting until the Zipcode value is 5... so all that happened is I passed it to another function? Or maybe I am stuck on how to get the length of the value of a node item? Please help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your event listener you are adding it to the document instead of each element separately

for (var i = 0; i < zipId.length; i++) {
                zipId[I].addEventListener("keyup", (e) => {
                    if (zipId[i].value.length == 5) {
                        GetCountyAndStateFromIds(zipId[i], countyId[i], stateId[i], phoneId[i]);
                    }
                });
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...