2009-04-29 38 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]

相关问题