简单的javascript只能在FireFox中使用

Nic*_*ams 2 javascript cross-browser

我创建了一个脚本,需要选择一个开始年份,然后只显示从开始年份开始的年份 - > 2009年.它只是一个startYear to endYear Range选择器.

该脚本仅适用于Firefox.我正在学习javascript,所以我希望有人能指出我正确的方向.可以在http://motolistr.com找到实时脚本

<script type="text/javascript">
function display_year2(start_year) {
//alert(start_year);
if (start_year != "Any") {
    var i;
    for(i=document.form.year2.options.length-1;i>=0;i--)
    {
        document.form.year2.remove(i);
    }

    var x = 2009;
    while (x >= start_year) {
        var optn = document.createElement("OPTION");
        optn.text = x;
        optn.value = x;
        document.form.year2.options.add(optn);
        //alert(x);
        x--;
    }
}
else 
{
    var i;
    for(i=document.form.year2.options.length-1;i>=0;i--)
    {
        document.form.year2.remove(i);
    }
    var optn = document.createElement("OPTION");
    optn.text = "Any";
    optn.value = "Any";
    document.form.year2.options.add(optn);
} // end else
} // end function
</script>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢,尼克

Mar*_*own 5

您正尝试在每个选项上设置onclick事件.IE不支持这一点.请尝试使用select元素中的onchanged事件.

而不是这个:

<select name="year1" id="year1">
    <option value="Any" onclick="javascript:display_year2('Any');" >Any</option>
    <option value="2009" onclick="javascript:display_year2(2009);" >2009</option>
    <option value="2008" onclick="javascript:display_year2(2008);" >2008</option>
</select>
Run Code Online (Sandbox Code Playgroud)

试试这个:

<select name="year1" id="year1" onchange="javascript:display_year2(this.options[this.selectedIndex].value)">
    <option value="Any">Any</option>
    <option value="2009">2009</option>
    <option value="2008">2008</option>
</select>
Run Code Online (Sandbox Code Playgroud)