0

我想仅使用 javascript 在选择/选项下拉列表中选择一个值。

修改 抱歉混淆。这是修改后的代码。

document.getElementById("sel_activity").value = activity;

这也可以,但是会更长一些..

<select id='sel_activity'>
<option value='CYC'>Cycle Count</option>
<option value='INV'>Inventory Count</option>
<option value='INVDROP'>Inventory Drop off</option>
<option value='INVPICK'>Inventory Pick up</option>
<option value='TRAIN'>Training</option>
</select>

<script type="text/javascript">

var select = 'CYC';
var myselect=document.getElementById("sel_activity")
for (var i=0; i<myselect.options.length; i++){
 if (myselect.options[i].value == select)
 {
    myselect.options[i].selected = true;
 }
 else
 {
    myselect.options[i].selected = false;
 } 
} 
</script>
4

2 回答 2

0

using jQuery

$("#sel_activity").val("CYC")

jQuery is a lightweight javasscript library which is very helpful in DOm traveral,event handling and ajax intearactions.

http://jquery.com/

Adding jQuery to your page is simple as adding one line of code in the head section to include the jQuery library

<head>
  <script type="text/javascript" src="h ttp://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
于 2010-10-04T13:43:04.913 回答
0
var myselect=document.getElementById("sel_activity")
myselect.options[myselect.options.length]=new Option("text","value");

http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/option.html

于 2010-10-04T13:39:49.440 回答