2009-02-03 26 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