2015-10-13 120 views
-2

所以我有两个下拉菜单,我的问题是第二个下拉菜单会自动选择第一个下拉菜单选择的JavaScript功能?自动复制所选第一个下拉菜单的下拉菜单

这里是我的代码

<select name="b" maxlength=2 style="width:70px; height:30px;" autocomplete="off" required/> 
 
\t <option>NMG</option> 
 
    <option>PRI</option> 
 
\t <option>FMNIC</option> 
 
\t </select> 
 

 
<select name="b" maxlength=2 style="width:70px; height:30px;" autocomplete="off" required/> 
 
\t <option>NMG</option> 
 
    <option>PRI</option> 
 
\t <option>FMNIC</option> 
 
\t </select>

回答

2

您可以使用平变化,

<select name="b" maxlength=2 style="width:70px; height:30px;" autocomplete="off" required onChange="setData(this);"/> 
<option>NMG</option> 
<option>PRI</option> 
<option>FMNIC</option> 
</select> 

<select name="b" Id="second" maxlength=2 style="width:70px; height:30px;" autocomplete="off" required/> 
<option>NMG</option> 
<option>PRI</option> 
<option>FMNIC</option> 
</select> 

Javascript

function setData(ctl) 
{ 
    document.getElementById("second").value = ctl.value; 
} 
1

jQuery让这很简单...

$(function() { 
    // Bind the onchange event of the 1st select. 
    $('select').eq(0).change(SelectionChanged); 
}); 

function SelectionChanged() { 
    var selectedValue = $('select').eq(0).val(); 
    $('select').eq(1).val(selectedValue ); 
}