2010-09-19 37 views
1

这段代码在firefox中正在工作。在IE中,警报是空的。IE中元素的空值

<select id="ronny" name="ronny" onchange="AjaxPost();alert(document.getElementById('ronny').value);"> 
    <option id="selected_ronny">All</option> 
    <?php 
     foreach($d_ronny as $ronny) 
     { 
      if ($ronny == $_POST['ronny_select']) 
      { 
       echo "<option selected id='selected_ronny'>$ronny</option>"; 
      } 
      else 
      { 
       echo "<option>$ronny</option>"; 
      } 
     } 
    ?> 
</select> 

选项有狐狸例如: All abc 123 xyz 当我选择xyz,警报显示xyz。在IE中,警报是空的。

谢谢!

+0

你能张贴AjaxPost代码( )? – xil3 2010-09-19 09:45:44

+0

该帖子很好。问题在于选择的价值。 – Ronny 2010-09-19 09:46:59

+0

如果您删除对'AjaxPost();的调用,您可能会发现它的工作原理,所以我们需要明白为什么这会中断。 – Codesleuth 2010-09-19 09:51:30

回答

0

对于onchange属性,你必须像这样的代码:

onchange="AjaxPost();alert(this.options[selectedIndex].value);" 

如果你想使用的ID,由document.getElementById('ronny')更换thi S:

onchange="AjaxPost();alert(document.getElementById('ronny').options[selectedIndex].value);" 
+0

谢谢!但我有一个问题..我想在其他JS使用选定的索引,我有一些。所以我如何使用ID做到这一点? – Ronny 2010-09-19 10:01:16

+0

@chelmertz:感谢您的编辑。 @Ronny:我已经更新了答案。 – Toto 2010-09-19 10:30:32

+0

谢谢!但我在ie和ff中有错误 - ''selectedIndex'未定义' – Ronny 2010-09-19 10:43:08