1
  • ここに2つのリストフィールドメニューがあります。最初の brand2番目item
  • 選択すればそれbrand IBMも選択itemするIBM
  • 一方、選択すると、それも選択brand HPitemHPます

javascriptでそれを行う方法。

<select name="brand">
  <option>Please Select</option>
  <option>IBM</option>
  <option>HP</option>
</select>

<select name="item">
  <option>Please Select</option>
  <option>IBM</option>
  <option>HP</option>  
</select>
4

3 に答える 3

3

オプションが互いに並んでいることに気付いたので、selectedIndex を最初から 2 番目に単純に反映できます。

document.getElementById("brand").onchange = function(){
  document.getElementById("item").selectedIndex = this.selectedIndex;
}
于 2009-12-29T14:20:35.380 に答える
2
 <select name="brand" id="brand" onchange="document.getElementById('item').value = document.getElementById('brand').value">
   <option>Please Select</option>
   <option>IBM</option>
   <option>HP</option>
 </select>

 <select name="item" id="item">
   <option>Please Select</option>
   <option>IBM</option>
   <option>HP</option>  
 </select>
于 2009-12-29T14:19:58.547 に答える
1

ブランド選択要素にonchange属性を追加できます。ブランドが選択されると、selectItem関数が呼び出され、 item select 要素の値に一致するアイテムを選択できます。また、document.getElementById("brand") を使用できるように、select 要素に ID を付与することをお勧めします。

<select id="brand" name="brand" onchange="selectItem(this.selectedIndex);">
  <option>Please Select</option>
  <option>IBM</option>
  <option>HP</option>
</select>

select 要素の DOM リファレンスは次のとおりです: http://www.w3schools.com/jsref/dom_obj_select.asp

于 2009-12-29T14:22:50.400 に答える