脚本在jsfiddle中工作,但在浏览器中不能用于自动填充下拉表单

时间:2014-09-17 12:44:58

标签: javascript jquery html forms

嗨请参阅下面的代码,了解我的自动填充下拉菜单。它在这个JSfiddle中运行正常但是由于某种原因,当我将代码放在我的网站上时,我没有选择第一个下拉选项,任何想法?这是小提琴= http://jsfiddle.net/fvL0qgrx/3/

<select id="device"></select>

<select id="item"></select>

<select id="product"></select> 

脚本

var products = [
    { label: 'BP-1 Battery Pack for Xbox Controller (BP1XB3-11)', category: 'Charge', devices: ['Xbox 360'] },
    { label: 'BP-2 PLAY & CHARGE BATTERY KIT (BP2XB1-11)', category: 'Charge', devices: ['Xbox One'] },
    { label: 'BP-3 Battery Power Pack for Mobile Devices (BP3MOB-11)', category: 'Charge', devices: ['Mobile'] },
    { label: 'DC-1 DUAL CHARGING DOCK (DC1PS4-11)', category: 'Charge', devices: ['PS4'] },
    { label: 'AC-1 Ammo Clip USB for PS3 (DFCPS3-22)', category: 'Charge', devices: ['PS3   '] },
    { label: 'AC-1 Ammo Clip USB for PS3 (Street King) (DFCPS3-23)', category: 'Charge', devices: ['PS3'] },
    { label: 'AC-1 Ammo Clip USB for Xbox 360 (DFCXB3-22)', category: 'Charge', devices: ['Xbox 360'] },
    { label: 'AC-1 Ammo Clip USB for Xbox 360 (Street King) (DFCXB3-23)', category: 'Charge', devices: ['Xbox 360'] },
    { label: 'EX1-RS Universal Mobile Headset BT3.0 (E1RMOB-11)', category: 'Headset', devices: ['Mobile'] },
    { label: 'EX1-RT Universal Mobile Headset With Noise Cancellation (E1RMOB-21)', category: 'Headset', devices: ['Mobile'] },
    { label: 'EX-03R WIRED INLINE MESSENGER HEADSET FOR PS4 (E3RPS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'EX-05S WIRED HIGH DEFINITION STEREO HEADSET FOR PS4 (E5SPS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'EX-05S Universal Wired Stereo Headset (E5SUNI-11)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05S Universal Wired Stereo Headset (Street King) (E5SUNI-12)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05S Wired Stereo Headset (PC, MAC, PS3, PS4,XB360) (E5SUNI-13)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05S Universal Wireless Stereo Headset  (E5SUNI-21)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05S Wireless Stereo Headset (E5SUNI-22)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'Elite Essentials Pack For Xbox 360 (ELPXB3-11)', category: 'Bundle Pack', devices: ['Xbox 360'] },
    { label: 'EX-01 Bluetooth Headset For PS3 (EX1PS3-11)', category: 'Headset', devices: ['PS3'] },
    { label: 'EX-02s Next Gen Bluetooth  Headset for PS3 (Red) camo (EX2PS3-11)', category: 'Headset', devices: ['PS3'] },
    { label: 'EX-02s Next Gen Bluetooth  Headset for PS3 (Black) camo (EX2PS3-12)', category: 'Headset', devices: ['PS3'] },
    { label: 'EX-03 Bluetooth Headset For PS3 (Blue pad printing) (EX3PS3-11)', category: 'Headset', devices: ['PS3'] },
    { label: 'EX-03 Bluetooth Headset for PS3 (Street King) (EX3PS3-12)', category: 'Headset', devices: ['PS3'] },
    { label: 'EX-03 WIRED INLINE MESSENGER HEADSET FOR PS4(EX3PS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'EX-03 Wired Headset For Xbox 360 (EX3XB3-11)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'EX-03 Wired Headset For Xbox 360 (Street King)(EX3XB3-12)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'EX-03 Wired Headset For Xbox 360 (EX3XB3-13)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'EX-05 Universal Wireless Stereo Headset (EX5UNI-22)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05 Universal Wired Stereo Headset (black version) (EX5UNI-44)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05 Lite Gaming Headset (EX5UNI-61)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'EX-05 Wired Headpset For Xbox 360(EX5XB3-22)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'EX-05 Lite Headset For Xbox 360 (EX5XB3-61)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'EX-06 FOLDABLE WIRELESS STEREO HEADSET (EX6PS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'EX-06 Wired Foldable Headset for Tablet (EX6TAB-11)', category: 'Headset', devices: ['Tablet'] },
    { label: 'EX-06 Wireless Gaming Foldable Headset (EX6UNI-21)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'FR-1 Freedom Wheel (FR1PS3-11)', category: 'Controller', devices: ['PS3'] },
    { label: 'GC-2 Bluetooth Controller (GC2PS3-11)', category: 'Controller', devices: ['PS3'] },
    { label: 'GC-2 Wireless RF Controller (GC2PS3-21)', category: 'Controller', devices: ['PS3'] },
    { label: 'GC-2 Wireless RF Controller (GC2PS3-22)', category: 'Controller', devices: ['PS3'] },
    { label: 'GC-2 Wireless RF Controller (Street King) (GC2PS3-23)', category: 'Controller', devices: ['PS3'] },
    { label: 'GC-2 Bluetooth Controller(Wii U)-white (GC2WIU-11)', category: 'Controller', devices: ['Wii U'] },
    { label: 'GC-2 Bluetooth Controller(Wii U)-black (GC2WIU-12)', category: 'Controller', devices: ['Wii U'] },
    { label: 'GC-2 Bluetooth Controller(Wii U)-black (GC2WIU-14)', category: 'Controller', devices: ['Wii U'] },
    { label: 'Gaming Starter Kit for PS3 (GSKPS3-11)', category: 'Bundle Pack', devices: ['PS3'] },
    { label: 'HC-1 ARMOUR WIRED STEREO HEADSET(HC1PS4-11)', category: 'Headset', devices: ['PS4'] } ,
    { label: 'HC-2 ARMOUR WIRED STEREO HEADSET (HC2PS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'HC-2 ARMOUR BLUETOOTH HEADSET(HC2PS4-31)', category: 'Headset', devices: ['PS4'] },
    { label: 'HC-4 ARMOUR WIRED STEREO HEADSET (HC4PS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'HC-4 ARMOUR BLUETOOTH HEADSET (HC4PS4-31)', category: 'Headset', devices: ['PS4'] },
    { label: 'HCC WIRED MONO HEADSET (PS4)(HCCPS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'HC-3 ARMOUR WIRED STEREO HEADSET (HC3UNI-11)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'HC-5 ARMOUR WIRELESS STEREO HEADSET (HC5UNI-21)', category: 'Headset', devices: ['Multiformat'] },
    { label: 'HCC WIRED MONO HEADSET (PC)(HCCPC-11)', category: 'Headset', devices: ['PC'] },
    { label: 'HCC WIRED MONO HEADSET (XB3)(HCCXB3-11)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'HS-1 WIRED STEREO HEADSET FOR PS4(HS1PS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'Stereo Wii-U Headset Black (HS1WIU-12)', category: 'Headset', devices: ['Wii U'] },
    { label: 'HD-1 HIGH SPEED HDMI CABLE WITH ETHERNET (HD1UNI-11)', category: 'Cable', devices: ['Multiformat'] },
    { label: 'Luminate for PS3 Slim (L8RPS3-11)', category: 'Lighting', devices: ['PS3'] },
    { label: 'PS3 light bar (LN8PS3-11)', category: 'Lighting', devices: ['PS3'] },
    { label: '3DS  Multi Car Chargeing Adaptor & Earbuds (MCCNDS-11)', category: 'Charge', devices: ['Nintendo 3DS'] },
    { label: 'Gaming Mega Pack For PS3 (MGPPS3-21)', category: 'Bundle Pack', devices: ['PS3'] },
    { label: 'MH-1 WIRED INLINE MESSENGER HEADSET (MH1PS4-11)', category: 'Headset', devices: ['PS4'] },
    { label: 'MH-1 Online Gaming Wired Headset (MH1XB3-11)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'Messenger Power Kit For Xbox 360 (MPKXB3-11)', category: 'Charge', devices: ['Xbox 360'] },
    { label: 'Online Starter Kit For PS3 (OSKPS3-11)', category: 'Bundle Pack', devices: ['PS3'] },
    { label: 'Online Starter Kit For PS3 (OSKPS3-21)', category: 'Bundle Pack', devices: ['PS3'] },
    { label: 'DS  Mains Power Supply Unit (PSU3DS-11)', category: 'Charge', devices: ['Nintendo 3DS'] },
    { label: 'UK MAINS POWER SUPPLY UNIT (DSI, 3DS, 3DSXL, 2DS) (PSU3DS-12)', category: 'Charge', devices: ['Nintendo 3DS'] },
    { label: 'RC-1 Gaming Bean Bag (RC1UNI-11)', category: 'Chair', devices: ['Multiformat'] },
    { label: 'RC-1 Gaming Bean Bag (RC1UNI-13)', category: 'Chair', devices: ['Multiformat'] },
    { label: 'RC-3 WIRED CHAIR (UK adaptor) (RC3UNI-11)', category: 'Chair', devices: ['Multiformat'] },
    { label: 'RC-3 WIRED CHAIR (interchangeable adaptors) (RC3UNI-12)', category: 'Chair', devices: ['Multiformat'] },
    { label: 'RC-5 Gaming Chair (RC5UNI-11)', category: 'Chair', devices: ['Multiformat'] },
    { label: 'Real Triggers (RT2PS3-23) ', category: 'Controller', devices: ['PS3'] },
    { label: 'Real Triggers (gold) (RT2PS3-25)', category: 'Controller', devices: ['PS3'] },
    { label: 'SC1 RF Wireless Sports Controller (SC1PS3-11)', category: 'Controller', devices: ['PS3'] },
    { label: 'SC1 RF Wireless Sports Controller (SC1PS3-12)', category: 'Controller', devices: ['PS3'] },
    { label: '3DS  Screen Protector Kit (SP13DS-11)', category: 'Cleaner', devices: ['Nintendo 3DS'] },
    { label: '3DS  Retractable Stylus x 2 (STYNDS-11)', category: 'Stylus', devices: ['Nintendo 3DS'] },
    { label: 'TX-2 Throat Mic For Xbox 360 (TX2XB3-11)', category: 'Headset', devices: ['Xbox 360'] },
    { label: 'VX-1 Wired  Controller For PS3  (VX1PS3-21)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-1 Wireless BT Controller For PS3 (VX1PS3-31)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-1 Wireless Controller for PS3  [No Battery] (VX1PS3-41)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-2 Wired Controller for PS3 (VX2PS3-11)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-2 Wired Controller for PS3 (BLUE) (VX2PS3-12)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-2 Wired Controller for PS3 (RED) (VX2PS3-13)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-2 RF Wireless Controller for PS3 (VX2PS3-21)', category: 'Controller', devices: ['PS3'] },
    { label: 'VX-2 BLUETOOTH Controller for PS3 (VX2PS3-31)', category: 'Controller', devices: ['PS3'] },
    { label: 'Wireless Adaptor for Xbox 360 (WA1XB3-11)', category: 'Cable', devices: ['Xbox 360'] },
    { label: 'XC-1 USB Play & Charge Cable For PS3 (XC1PS3-21)', category: 'Cable', devices: ['PS3'] },
    { label: 'XC-1 USB BLUE PLAY & CHARGE CABLE (XC1PS4-11)', category: 'Cable', devices: ['PS4'] },
    { label: 'XC-1 USB  GREEN PLAY & CHARGE CABLE (XC1XB1-11)', category: 'Cable', devices: ['Xbox 360'] },
    { label: 'XC-3 HQ High Speed HDMI Cable For PS3 (XC3PS3-21)', category: 'Cable', devices: ['PS3'] },
    { label: 'XC-3 HQ High Speed HDMI Cable For Xbox 360 (XC3XB3-21)', category: 'Cable', devices: ['Xbox 360'] },
    { label: 'XC-4 HQ High Speed HDMI Cable With Ethernet (XC4UNI-21)', category: 'Cable', devices: ['Multiformat'] },
    { label: 'XC-5 HIGH SPEED HDMI CABLE WITH ETHERNET (XC5PS4-11)', category: 'Cable', devices: ['PS4'] },
    { label: 'XC-5 HIGH SPEED HDMI CABLE WITH ETHERNET (XC5UNI-11)', category: 'Cable', devices: ['Multiformat'] },
    { label: 'XC-6 OPTICAL CABLE (XC6UNI-11)', category: 'Cable', devices: ['Multiformat'] },
    { label: 'MICRO USB TO HDMI CABLE ANDROID (XCMMOB-11)', category: 'Cable', devices: ['Mobile'] },
];


var devices = [];
_.forEach(products, function(product){
    _.forEach(product.devices, function(device){
        if(devices.indexOf(device) === -1){
            devices.push(device);
        };
    });
});

var getCategoriesForDevice = function(device){
    var categories = _.chain(products)
        .filter(function(product){
            return product.devices.indexOf(device) !== -1;
        })
        .map(function(product){
            return product.category
        })
        .uniq()
        .value();
    return categories;
};


var getProductsForDeviceCategory = function(device, category){
    return _.filter(products, function(product){
        return product.category === category && product.devices.indexOf(device) !== -1;
    });
};



$(function() {

    var $device = $('#device');
    var $item = $('#item');
    var $product = $('#product');

    $device.html('<option value=></option>');
    _.forEach(devices, function(device){
        $device.append('<option>' + device + '</option>');
    });

    $device.on('change', function(evt){
        var $el = $(this);
        var value = $el.val();
        var data = [];
        if(value){
            data = getCategoriesForDevice(value);
        }
        $item.html('<option value=></option>');
        _.forEach(data, function(option){
            $item.append('<option>' + option + '</option>');
        }); 
    });

    $item.on('change', function(evt){
        var $el = $(this);
        var value = $el.val();
        var data = [];
        if(value){
            data = getProductsForDeviceCategory($device.val(), value);
        }
        $product.html('<option val=></option>');
        _.forEach(data, function(option){
            $product.append('<option>' + option.label + '</option>');
        }); 
    });

});

0 个答案:

没有答案