解决问题的第一步就是分解问题: <select id="select_temp" onchange="changeOptions()"></select> <script> function changeOptions() { var select_temp = document.getElementById("select_temp"); var value = select_temp.selectedIndex; while (select_temp.firstChild) { select_temp.removeChild(select_temp.firstChild); } for (var i = 0; i < 10; i++) { var option = new Option(i, i); if (i == value) option.selected = true; select_temp.options.add(option); } } changeOptions(); </script> 这样就重新了楼主的问题。 关键的地方:在IE6下,onchange事件中设置添加的option.selected无效。可通过设置selectedIndex解决此问题。 <select id="select_temp" onchange="changeOptions()"></select> <script> function changeOptions() { var select_temp = document.getElementById("select_temp"); var value = select_temp.selectedIndex; while (select_temp.firstChild) { select_temp.removeChild(select_temp.firstChild); } for (var i = 0; i < 10; i++) { var option = new Option(i, i); select_temp.options.add(option); } select_temp.selectedIndex = value; } changeOptions(); </script>正如我楼上所说,最简单和合理的方法就是选择天数时不用重新生成options
你们那边使用IE6都正常?难不成只有我们公司的IE6不正常?
var setElDate = function (noChangeOption) {
var yVal = $(sltYear).val(),
mVal = $(sltMonth).val(),
dVal = $(sltDay).val();
el.val(yVal + "-" + mVal + "-" + dVal);
if (noChangeOption) return;
jMiniCalendar.innerOption(sltDayId, 1, jMiniCalendar.getMaxDay(yVal, mVal), dVal);
}; $(sltDay).change(function () {
return setElDate(true);
});
<select id="select_temp" onchange="changeOptions()"></select>
<script>
function changeOptions() {
var select_temp = document.getElementById("select_temp");
var value = select_temp.selectedIndex;
while (select_temp.firstChild) {
select_temp.removeChild(select_temp.firstChild);
}
for (var i = 0; i < 10; i++) {
var option = new Option(i, i);
if (i == value) option.selected = true;
select_temp.options.add(option);
}
}
changeOptions();
</script>
这样就重新了楼主的问题。
关键的地方:在IE6下,onchange事件中设置添加的option.selected无效。可通过设置selectedIndex解决此问题。
<select id="select_temp" onchange="changeOptions()"></select>
<script>
function changeOptions() {
var select_temp = document.getElementById("select_temp");
var value = select_temp.selectedIndex;
while (select_temp.firstChild) {
select_temp.removeChild(select_temp.firstChild);
}
for (var i = 0; i < 10; i++) {
var option = new Option(i, i);
select_temp.options.add(option);
}
select_temp.selectedIndex = value;
}
changeOptions();
</script>正如我楼上所说,最简单和合理的方法就是选择天数时不用重新生成options