下拉值应根据单选按钮更改

时间:2014-04-16 10:42:15

标签: javascript php

我需要下拉选择值会根据两个单选按钮动态变化。例如,在我的html代码中,如果从国家/地区选择国家/地区并从城市中选择city1,则下拉值将设置为1 如果从国家/地区选择国家1并从城市中选择city2,则下拉值将设置为2。 我需要JavaScript或PHP代码。

感谢您的帮助。

<div class='aQuestion' id='div1'>
    <STRONG>1. </STRONG>
    <STRONG>Country</STRONG></br>
    <INPUT TYPE='radio' NAME='grp1' VALUE='Country 0'>Country1</br>



</div>

<div class='aQuestion' id='div2'>
    <STRONG>2. </STRONG>
    <STRONG>City</STRONG></br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='City 0'>City 1</br>
    <INPUT TYPE='radio' NAME='grp2' VALUE='City1'>City 2</br>


</div><br/>

<div> set 1 
<select name="school">

<option value="school1">school 1</option>;
<option value="school2">school 2</option>;
<option value="school3">school 3</option>;


</select><br/>
</div><br/>


<div>
 set 2
<select name="school">

<option value="school4">school 4</option>;
<option value="school5">school 5</option>;
<option value="school6">school 6</option>;

</select>
</div>

1 个答案:

答案 0 :(得分:0)

替换

<INPUT TYPE='radio' NAME='grp2' VALUE='City 0'>City 1</br>
<INPUT TYPE='radio' NAME='grp2' VALUE='City1'>City 2</br>

 <INPUT TYPE='radio' NAME='grp2' VALUE='City 0' onClick="javascript:return yourfunction(1)">City 1</br>
 <INPUT TYPE='radio' NAME='grp2' VALUE='City1' onClick="javascript:return yourfunction(2)">City 2</br>


JavaScript代码

function yourfunction(radioid)
{
  if(radioid == 1)
  {    
document.getElementById('one').style.display = '';
    document.getElementById('two').style.display = 'none';
  }
  else if(radioid == 2)
  {  
   document.getElementById('two').style.display = '';
   document.getElementById('one').style.display = 'none';
  }
}

工作模式,请参阅

http://jsfiddle.net/NWWL4/23/

希望这会有所帮助