当第二个重置为0时,重置第一个选择列表

时间:2014-02-18 18:31:32

标签: javascript html list select

我有一些示例代码,我想要的是当第二个选择列表中的值设置为0时,第一个也重置为0,当它们在一个中选择一个值时彼此重置。任何帮助表示感谢:

HTML:

<select name="Standard" id="std" size="6" onClick="selectCheck(this,'del'); return true;"> 
     <option value="0">Select one</option> 
     <option value="Eggs" selected>Eggs</option> 
     <option value="Bacon">Bacon</option> 
     <option value="Toast">Toast</option> 
     <option value="Ham">Ham</option> 
     <option value="Home">Home Fries</option> 
     <option value="Jelly">Jelly</option> 
</select> 
<select name="Deluxe" id="del" size="6" onClick="selectCheck(this,'std'); return true;"> 
     <option value="0">Select one</option> 
     <option value="Omelet">Omelet</option> 
     <option value="Canadian">Canadian Bacon</option> 
     <option value="Muffin">Muffin</option> 
     <option value="Steak">Steak</option> 
     <option value="Cottage">Cottage Fries</option> 
     <option value="Preserves">Preserves</option> 
</select>

JS:

function selectCheck(me,other) { 
    var ind = me.selectedIndex; 
    if (ind > 0) { 
       document.getElementById(other).selectedIndex = 0; 
    } 
 } 

2 个答案:

答案 0 :(得分:0)

查找特定ID

if (ind > 0 && other !='del') { 
  document.getElementById(other).selectedIndex = 0; 
 } 

<强> DEMO

答案 1 :(得分:0)

<select name="Standard" id="std" size="6"> 
<option value="0">Select one</option>
 <option value="Eggs" selected>Eggs</option>
<option value="Bacon">Bacon</option> 
<option value="Toast">Toast</option> 
<option value="Ham">Ham</option> 
<option value="Home">Home Fries</option> 
<option value="Jelly">Jelly</option> </select>
 <select name="Deluxe" id="del" size="6" onClick="selectCheck(this,'std'); return true;"> 
<option value="0">Select one</option> 
<option value="Omelet">Omelet</option> 
<option value="Canadian">Canadian Bacon</option>
 <option value="Muffin">Muffin</option>
 <option value="Steak">Steak</option> 
<option value="Cottage">Cottage Fries</option> 
<option value="Preserves">Preserves</option> 
</select>
<script>
function selectCheck(me,other) { var ind = me.selectedIndex; if (ind<1) { document.getElementById(other).selectedIndex = 0; } }
</script>