Javascript设置选定的选项

时间:2015-07-22 00:00:27

标签: javascript html select html-select

我正在尝试使用以下代码以编程方式设置<select>选项。正在为选择设置该值,但该选项未反映它。任何帮助将不胜感激。

设置选择值:

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,但第二航空分队是显示的选项。

1 个答案:

答案 0 :(得分:0)

试试这个:

document.getElementById('cunit_usergroup').value='3';
祝你好运!!

jsFiddle demo