2009-04-29 108 views
1

這一點的HTML和Javascript工作在IE6,FF2和FF3。我找不到任何理由爲什麼它不應該在IE7工作也行,不過this.selectedIndex總是返回0爲什麼this.selectedIndex無法在IE7上使用<select>標籤?

** in javascript file 
function onTypeChange() 
{ 
    alert($('type_id').selectedIndex); 
    if ($('type_id').selectedIndex != 0) 
    { 
     Element.update('chosenType', this.options[this.selectedIndex].text);  
     Form.Element.enable('go_button'); 
    } else { 
     Element.update('chosenType', 'Selected Type'); 
     Form.Element.disable('go_button'); 
    } 
} 

** in html 
<select class="hosp_select_buttons selectbox" id="type_id" name="type[id]" 
     onchange="onTypeChange();"> 
<option value="">Please select</option> 
<option value="1594">Ambulatory Surgical Center</option> 
<option value="1595">Birthing Center</option> 
<option value="1596">Comprehensive Outpatient Rehabilitation Facilities</option> 
<option value="1597">Drug Abuse Treatment Program</option> 
<option value="1598">Mammography</option> 
<option value="1599">Narcotic Treatment Program</option> 
<option value="1600">Outpatient Physical Therapy</option> 
<option value="1601">Private Home Care Provider</option></select> 

**編輯改變風格的東西人反對如此強烈過。警告仍然表示在更改選擇框後selectedIndex爲0。該代碼已經並且仍然可以在除I.E以外的所有瀏覽器中使用。 7

+0

是否有任何的是JS的評價?我從來沒有見過那麼多的JS投入onchange。通常情況下,它是附加的或放入一個函數。 – 2009-04-29 18:48:38

+0

提醒您http://thedailywtf.com/Articles/OnClick-Does-What!.aspx? – Greg 2009-04-29 18:53:21

+0

因爲2行javascript == 30行表單驗證碼? – 2009-04-29 20:13:22

回答

2

我知道這是老了,但我不能不看到在這裏unaswered。我認爲這裏的問題是你使用$('type_id'),它返回jQuery中的元素(我相信)。要訪問實際的HTML元素,你必須使用$('type_id')[0]或類似的東西。我認爲如果你使用document.getElementById('type_id')它應該工作。

編輯:更改答案,以反映Benxamin的有關如何訪問( 'TYPE_ID')的DOM元素$ [0]

相關問題