gpt4 book ai didi

javascript - IE8 选择选项时出错

转载 作者:行者123 更新时间:2023-12-03 08:02:39 26 4
gpt4 key购买 nike

我有一个带有两个选择区域和状态的表单,以及带有区域的过滤器状态。我的脚本工作正常,但在 IE8 中出现此错误。为什么在 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']
]
};

关于javascript - IE8 选择选项时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34527066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com