0

簡単なコード:

<input type="radio" id="knob-reassign-leave" name="knob-reassign" value="n" checked="checked">Leave <br>
<input type="radio" id="knob-reassign-cmp" name="knob-reassign" value="d"> Default <br>
<input type="radio" id="knob-reassign" name="knob-reassign" value="r"> Reassign to

<select name="assigned_to" id="assigned_to" onchange="
  if ((this.value != 'currentuser') && (this.value != '')) {
    var kr = document.getElementById('knob-reassign');
    document.getElementById('knob-reassign').checked=true;
  }">

  <option value="otheruser">Someone Else</option>
  <option value="lastuser">Someone Third</option>
  <option value="currentuser" selected="selected">Me</option>
</select>

これはすべて FF と chrome では非常にうまく機能しますが、コードを書くときによく耳にするように、これは IE では機能しません。IE が ID の「名前」を検索しているか、ID を名前に変換しようとしているように見えます。これは、最後のラジオを取得する必要があるときに常に最初のラジオを選択するためです。

4

1 に答える 1

0

<input>名前とIDが同じ場合、IE6/7/8は実際に最初の要素を選択するようです。簡単なテストケースを次に示します。

<input type="radio" id="a" name="c" value="1">
<input type="radio" id="b" name="c" value="2">
<input type="radio" id="c" name="c" value="3">

<script type="text/javascript">
    // Should get 3rd <input> with id 'c' and alert '3'
    // Instead finds 1st input with name 'c' and alerts '1'
    alert(document.getElementById('c').value); 
</script>

説明を提供することはできませんが ( Google には関連する結果がたくさんあります)、修正を提供できます。IE の getElementById の実装をオーバーライドするか、最後の要素の ID を名前とは異なるものに変更します。

<input type="radio" id="knob-reassign-leave" name="knob-reassign" value="n" checked="checked">Leave <br>
<input type="radio" id="knob-reassign-cmp" name="knob-reassign" value="d"> Default <br>
<input type="radio" id="knob-reassign-reassign" name="knob-reassign" value="r"> Reassign to

<select name="assigned_to" id="assigned_to" onchange="
  if ((this.value != 'currentuser') && (this.value != '')) {
    document.getElementById('knob-reassign-reassign').checked=true;
  }">

  <option value="otheruser">Someone Else</option>
  <option value="lastuser">Someone Third</option>
  <option value="currentuser" selected="selected">Me</option>
</select>
于 2011-02-03T14:58:45.330 に答える