简体   繁体   中英

Why is this for loop returning the index of a character and not the whole property?

var options = {
fieldsToValidate : {},  
slideDowns : {
    0: '#address-phone-block',
    1: '#zip-dob-block',
    2: '#nextButton'
},
continueButton: "#continue-button",
landingForm: '#landingForm'
}

for (var slider in options.slideDowns[]){
    console.log('did this work?' , slider , options.continueButton[slider])
    $(options.continueButton[slider]).slideDown();  
}

When the console logs i get this output, and then this error on the jQuery selector:

did this work? 0 #
"Syntax error, unrecognized expression: #"

I know that slider is 0/1/2 so why is this giving me the first character of the first property?

As options.continueButton is String with ' #continue-button ' value, so options.continueButton[0] is its first character, which is ' # '.

Probably you need to use options.slideDowns instead of options.continueButton , as you are looping through its properties:

for (var slider in options.slideDowns){
    console.log('did this work?' , slider , options.slideDowns[slider])
    $(options.slideDowns[slider]).slideDown();  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM