2009-02-03 72 views
2

我创建了一个脚本,需要选择一个开始年份,然后只显示年份从开始年份 - > 2009年。这只是一个startYear结束年份范围选择器。简单的JavaScript只在FireFox中工作

该脚本只适用于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> 

任何想法?

感谢, 尼克

回答

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> 

尝试这种情况:

<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> 
+0

感谢您的帮助,可以在所有浏览器中进行更改。我很高兴。 – 2009-02-03 11:34:54

+1

您不需要在`onchange`或`onclick`等事件属性中使用`javascript:`。那只是解释为标签。请参阅https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/label – Gumbo 2009-09-25 08:52:17

0

虽然这不是解决您的问题;您指的是输入字段的错误方式:document.form.year2应该是document.getElementById('year2')。我还注意到你的当前脚本在Opera中工作。

0

尝试手动设置长度

document.form.year2.options.length = number_of_items。

4

它不工作的原因是不是在你的代码片段:

<OPTION onclick=javascript:display_year2(2009); value=2009> 

Option.onclick不是普遍预期触发一个事件,但不会在Firefox。检测选择值更改的常用方法是通过Select.onchange。此外,您不需要在事件处理程序中包含“javascript:”,它们不是URL(也永远不会使用javascript:URL)。另外,引用你的属性值;它总是一个好主意,当你开始在值中包括标点符号时,它是必需的。另外,坚持你的值的字符串 - 你用一个数字调用display_year2,但option.value总是一个字符串;尝试使用混合数据类型是混淆的秘诀。

总结:

<select onchange="display_year2(this.options[this.selectedIndex].value)"> 
    <option value="2009">2009</option> 
    ... 
</select> 

其他的事情:

var i; 
for(i=document.form.year2.options.length-1;i>=0;i--) 
{ 
document.form.year2.remove(i); 
} 

您可以通过书面形式向options.length废除循环。另外最好避免直接从文档中引用元素名称 - 使用文档。形式[]集合或getElmentById:

var year2= document.getElementById('year2'); 
year2.options.length= 0; 

另外:

var optn = document.createElement("OPTION"); 
optn.text = x; 
optn.value = x; 
document.form.year2.options.add(optn); 

HTMLCollection.add不是标准DOM方法。传统的老派方式是这样的:

year2.options[year2.options.length]= new Option(x, x); 
1

我在onchange工作在Firefox的类似问题,但不是IE浏览器,有时不是Chrome。我在我的Head标签中添加了以下内容,并且所有内容都很好: meta http-equiv =“X-UA-Compatible”content =“ IE =边缘,镀铬= 1"

注:这样做是:总是强迫最新的IE渲染引擎(甚至在企业内部网)& Chrome Frame的BUT:如果您在此处使用的.htaccess