Question

I am trying to get javascript to print out the price of the phones but for some reason it wouldn't print. I have done a js lint test and I can't see why its not linking up. I was hoping some could point me in the right direction. Here is my jsfiddle. http://jsfiddle.net/blimbam/HEu55/1/.

Here is my Javascript code.

 var priceBooks = {
    'iphone': {
        'prepay': {
            '12': "n/a",
                '18': "n/a",
                '24': "n/a",
                'prepay': 619
        },
            'CFM': {
            '12': "n/a",
                '18': 229,
                '24': 129,
                'prepay': "n/a"
        },
            'Band B': {
            '12': "n/a",
                '18': 219,
                '24': 119,
                'prepay': "n/a"
        }
    },
    HTCOne: {
        prepay: {
            '12': "n/a",
                '18': "n/a",
                '24': "n/a",
                'prepay': 599
        },
            'CFM': {
            '12': "n/a",
                '18': 179,
                '24': 79,
                'prepay': "n/a"
        },
            'Band B': {
            '12': "n/a",
                '18': 169,
                '24': 69,
                'prepay': "n/a"
        }
    },
    S3: {
        'prepay': {
            '12': "n/a",
                '18': 'n/a',
                '24': 'n/a',
                'prepay': 569
        },
            'CFM': {
            '12': "n/a",
                '18': 79,
                '24': 0,
                'prepay': 'n/a'
        },
            'BandB': {
            '12': 'n/a',
                '18': 69,
                '24': 0,
                'prepay': 'n/a'
        }
    }
},

    phone = document.GetElementById('phone'),
    plan = document.GetElementById('plan'),
    term = document.GetElementById('term'),
    price = document.GetElementById('price');


function showPrice() {
    'use strict';
    if (term.value !== '') {
        price.value = priceBooks[phone.value][plan.value][term.value];
    } else {
        price.value = 'wrong input';
    }
}
phone.onchange = plan.onchange = term.onchange = showPrice;
Était-ce utile?

La solution

In addition to Xotic750's recommendation of changing GetElementById to getElementById, there are a couple of misplaced commas and semicolons. Here is my modified code:

var priceBooks = {
    'iphone': {
        'prepay': {
            '12': "n/a",
                '18': "n/a",
                '24': "n/a",
                'prepay': 619
        },
            'CFM': {
            '12': "n/a",
                '18': 229,
                '24': 129,
                'prepay': "n/a"
        },
            'Band B': {
            '12': "n/a",
                '18': 219,
                '24': 119,
                'prepay': "n/a"
        }
    },
    HTCOne: {
        prepay: {
            '12': "n/a",
                '18': "n/a",
                '24': "n/a",
                'prepay': 599
        },
            'CFM': {
            '12': "n/a",
                '18': 179,
                '24': 79,
                'prepay': "n/a"
        },
            'Band B': {
            '12': "n/a",
                '18': 169,
                '24': 69,
                'prepay': "n/a"
        }
    },
    S3: {
        'prepay': {
            '12': "n/a",
                '18': 'n/a',
                '24': 'n/a',
                'prepay': 569
        },
            'CFM': {
            '12': "n/a",
                '18': 79,
                '24': 0,
                'prepay': 'n/a'
        },
            'BandB': {
            '12': 'n/a',
                '18': 69,
                '24': 0,
                'prepay': 'n/a'
        }
    }
};

phone = document.getElementById('phone');
plan = document.getElementById('plan');
term = document.getElementById('term');
price = document.getElementById('price');

function showPrice() {
    'use strict';
    if (term.value !== '') {
        price.value = priceBooks[phone.value][plan.value][term.value];
    } else {
        price.value = 'wrong input';
    }
}
phone.onchange = plan.onchange = term.onchange = showPrice;

Autres conseils

It should read getElementById and not GetElementById, using the developer console would show you this problem as an error is thrown. CTRL-SHIFT-J will take you to the developer console on many browsers.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top