Chained Select with NO jQuery or Ajax

前端 未结 1 1359
Happy的楠姐
Happy的楠姐 2021-01-16 15:45

I\'m using XAMPP Lite - USB Version and found that the jQuery chained select boxes scripts don\'t work because they rely on AJAX which is not working on my XAMPP.

I

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-16 16:23

    here's a quick sample, not optimized but does the job. as said, no jQuery and no AJAX. this one works in standards compliant browsers, you man need to tweak it for IE coz i have no IE to test on.

    
     
    
    
         ​
    
    
    window.onload = (function() {
    
        var locations = {
            'p1': [
                'p1c1',
                'p1c2',
                'p1c3',
                ],
            'p2': [
                'p2c1',
                'p2c2',
                'p2c3',
                ],
            'p3': [
                'p3c1',
                'p3c2',
                'p3c3',
                ],
        };
    
        var provinces = document.getElementById('province');
        var cities = document.getElementById('city');
    
        provinces.addEventListener('change', function() {
            loadCities(this.value)
        }, false)
    
    
        var loadCities = (function loadCitiesFunc(key) {
            key = key || 'p1';
            var docFragment = document.createElement('select');
            for (var i = 0; i < locations[key].length; i++) {
                docFragment.appendChild(new Option(locations[key][i], locations[key][i]));
            }
            cities.innerHTML = docFragment.innerHTML;
    
            return loadCitiesFunc;
        }())
    
    }());​
    

    0 讨论(0)
提交回复
热议问题