2010-09-19 55 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