2015-10-13 145 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 ); 
}