2015-07-22 127 views
0

我正在嘗試使用下面的代碼以編程方式設置<select>的選項。值正在爲選擇設置,但該選項沒有反映出來。任何幫助將不勝感激。Javascript set selected

設置選擇值:

document.getElementById('cunit_usergroup').setAttribute('value', '3'); 

HTML:

<select id="cunit_usergroup" class="input_select" tabindex="1" name="cunit_usergroup" value="3"> 
    <option value="62">2nd Aviation Detachment</option> 
    <option value="3">3rd Infantry Division</option> 
    <option value="77">Alpha Company Headquarters</option> 
    <option value="4">Alpha Company Leadership </option> 
    <option value="5">Banned</option> 
    <option value="13">Base Maintenance Operations</option> 
    <option value="9">Civillian</option> 
    <option value="17">Combat Imaging and Development</option> 
    <option value="10">Corps of Engineers </option> 
    <option value="48">Enlistee's</option> 
    <option value="74">First Platoon Leadership</option> 
    <option value="50">First Platoon, First Squad</option> 
    <option value="61">First Platoon, Fourth Squad</option> 
    <option value="51">First Platoon, Second Squad</option> 
    <option value="46">First Platoon, Third Squad</option> 
    <option value="11">Force Improvement Group</option> 
    <option value="2">Guests</option> 
    <option value="63">PERSCOM System</option> 
    <option value="14">Public Affairs Office</option> 
    <option value="12">Recruiting and Retention Office</option> 
    <option value="49">Retired Personnel</option> 
    <option value="75">Second Platoon Leadership</option> 
    <option value="69">Second Platoon, First Squad</option> 
    <option value="72">Second Platoon, Fourth Squad</option> 
    <option value="70">Second Platoon, Second Squad</option> 
    <option value="71">Second Platoon, Third Squad</option> 
    <option value="76">Third Platoon Leadership</option> 
    <option value="52">Third Platoon, First Squad</option> 
    <option value="73">Third Platoon, Fourth Squad</option> 
    <option value="65">Third Platoon, Second Squad</option> 
    <option value="66">Third Platoon, Third Squad</option> 
    <option value="1">Validating</option> 
</select> 

正如你所看到的,值被設置爲3,但是,第二個航空支隊正在顯示的選項。

+0

你要設置'select' – tymeJV

回答

0

試試這個:

document.getElementById('cunit_usergroup').value='3'; 

祝你好運!

jsFiddle demo

+0

這並不工作,以及的'selectedIndex'。 –

+0

該演示適用於我。 –

+0

我不知道它是否與顯示在模態顯示窗口中的事實有關......它不適合我。我完全像你一樣。 –