Select 选项中的 IE8 错误

IE8 Error in Select Option

我有一个包含两个 Select 区域和状态的表单,并且还使用区域过滤状态。我的脚本工作正常,但在 IE8 中我有这个 error.why 这个错误发生在 IE8 中?

'0' is null or not an object

如何解决这个问题?

这是我的代码:

    <select id="regionId" name="region">
    <option value="Central">Central</option>
    <option value="Eastern">Eastern</option>
    <option value="Northeastern">Northeastern</option>
    <option value="Northern">Northern</option>
    <option value="Southern">Southern</option>
    <option value="Western">Western</option>
    </select>

<select id="stateId" name="state">
</select>



    <script type="text/javascript" >
    $(document).ready(function () {

    var countryStateArray = {
        'Central': [
            ['', 'State / Territory'],
            ['Madhya Pradesh', 'Madhya Pradesh'],
            ['Chhattisgarh', 'Chhattisgarh'],
        ],
            'Eastern': [
            ['', 'State / Territory'],
            ['Odisha', 'Odisha'],
            ['Bihar', 'Bihar'],
            ['West Bengal', 'West Bengal'],
            ['Jharkhand', 'Jharkhand'],
            ['Manipur', 'Manipur'],
            ['Mizoram', 'Mizoram'],
            ['Nagaland', 'Nagaland'],
            ['Tripura', 'Tripura'],
        ],
            'Northeastern': [
            ['', 'State / Territory'],
            ['Arunachal Pradesh', 'Arunachal Pradesh.'],
            ['Assam', 'Assam'],
            ['Meghalaya', 'Meghalaya'],
            ['Sikkim', 'Sikkim'],
        ],
        'Northern': [
         ['', 'State / Territory'],
            ['Uttar Pradesh', 'Uttar Pradesh'],
            ['Jammu and Kashmir++', 'Jammu and Kashmir++'],
            ['Himachal Pradesh', 'Himachal Pradesh'],
            ['Uttarakhand', 'Uttarakhand'],
            ['Punjab', 'Punjab'],
            ['Haryana', 'Haryana'],
            ['Delhi', 'Delhi'],
            ['Chandigarh', 'Chandigarh'],

        ],
        'Southern': [
      ['', 'State / Territory'],
            ['Karnataka', 'Karnataka'],
            ['Andhra Pradesh', 'Andhra Pradesh'],
            ['Tamil Nadu', 'Tamil Nadu'],
            ['Telangana', 'Telangana'],
            ['Kerala', 'Kerala'],
            ['Puducherry', 'Puducherry'],   
        ],
        'Western': [
      ['', 'State / Territory'],
            ['Rajasthan', 'Rajasthan'],
            ['Maharashtra', 'Maharashtra'],
            ['Gujarat', 'Gujarat'],
            ['Goa', 'Goa'],  
        ]


    };

function setStates() {
    var $country = $('#regionId');
    var $state = $('#stateId');
    var countryId = $country.val();
    $state.empty();
    if (countryStateArray[countryId]) {
        for (var i = 0; i < countryStateArray[countryId].length; i++) {
            var state = countryStateArray[countryId][i];
            $('<option />', {
                value: state[0],
                text: state[1]
            }).appendTo($state);
        }

        $state.show();
    } else {
        $state.empty().hide();
    }
}
$('#regionId').change(setStates); 

</script>

只需从数组定义中删除所有结尾的逗号:

var countryStateArray = {
    'Central': [
        ['', 'State / Territory'],
        ['Madhya Pradesh', 'Madhya Pradesh'],
        ['Chhattisgarh', 'Chhattisgarh']
    ],
        'Eastern': [
        ['', 'State / Territory'],
        ['Odisha', 'Odisha'],
        ['Bihar', 'Bihar'],
        ['West Bengal', 'West Bengal'],
        ['Jharkhand', 'Jharkhand'],
        ['Manipur', 'Manipur'],
        ['Mizoram', 'Mizoram'],
        ['Nagaland', 'Nagaland'],
        ['Tripura', 'Tripura']
    ],
        'Northeastern': [
        ['', 'State / Territory'],
        ['Arunachal Pradesh', 'Arunachal Pradesh.'],
        ['Assam', 'Assam'],
        ['Meghalaya', 'Meghalaya'],
        ['Sikkim', 'Sikkim']
    ],
    'Northern': [
     ['', 'State / Territory'],
        ['Uttar Pradesh', 'Uttar Pradesh'],
        ['Jammu and Kashmir++', 'Jammu and Kashmir++'],
        ['Himachal Pradesh', 'Himachal Pradesh'],
        ['Uttarakhand', 'Uttarakhand'],
        ['Punjab', 'Punjab'],
        ['Haryana', 'Haryana'],
        ['Delhi', 'Delhi'],
        ['Chandigarh', 'Chandigarh']

    ],
    'Southern': [
  ['', 'State / Territory'],
        ['Karnataka', 'Karnataka'],
        ['Andhra Pradesh', 'Andhra Pradesh'],
        ['Tamil Nadu', 'Tamil Nadu'],
        ['Telangana', 'Telangana'],
        ['Kerala', 'Kerala'],
        ['Puducherry', 'Puducherry']  
    ],
    'Western': [
  ['', 'State / Territory'],
        ['Rajasthan', 'Rajasthan'],
        ['Maharashtra', 'Maharashtra'],
        ['Gujarat', 'Gujarat'],
        ['Goa', 'Goa'] 
    ]
};