javascript中怎样添加下拉列表框的option到指定位置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript中怎样添加下拉列表框的option到指定位置相关的知识,希望对你有一定的参考价值。

下拉列表框中数据是已经绑定了的 我现在要用javascript添加一项到最前面(变成第一项)像下面一样
options.add(new Option("全部", ""));
请问如何实现?

参考技术A <script language="javascript">
function tianjia()

newopt=document.createElement("option");
newopt.text="0";
newopt.value="00";
document.getElementById("a").options.add(newopt,0);

</script>
<select id="a">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<input type="button" onclick="tianjia()">本回答被提问者采纳
参考技术B <body>
<select id='select'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</body>
<script>
var ele = document.getElementById('select');
var newOption = document.createElement('option');
var newSelect = document.createElement('select');
var optionArr=new Array();
var str='', num=0;
for (var i=0; i < ele.childNodes.length;i++)
if (ele.childNodes[i].innerhtml)
optionArr[num]=ele.childNodes[i];
num++;


newOption.innerHTML='0';
optionArr.reverse().push(newOption)
optionArr.reverse();
for (var i=0; i<optionArr.length; i++)
newSelect.appendChild(optionArr[i]);

newSelect.firstChild.selected=true;
ele.parentNode.replaceChild(newSelect,ele);
</script>
参考技术C <select name="select"id="select" style="width:150px;"onChange="if(this.value!=''&&this.value!='#')window.open(this.value);this.selectedIndex=0;">
<option>www</option>
参考技术D 用jQuery吧,下载个jquery1.4.0.1.js,用$(id).prepend(option); 第5个回答  2015-08-12 insert insertBefore

excel怎样添加的选项卡中含有下拉列表

 1 <customUI 
 2   xmlns="http://schemas.microsoft.com/office/2006/01/customui">
 3   <ribbon startFromScratch="false">
 4     <tabs>
 5       <tab id="myxxk"
 6         insertBeforeMso="TabHome"
 7         label="我的选项卡">
 8         <group id="cxgroup"
 9           label="查询">
10           <box id="rxbox1"
11             boxStyle="vertical">
12             <box id="rxbox11"
13               boxStyle="horizontal">
14               <comboBox 
15                 id="ks_n"
16                 label="开始日期:"
17                 onChange="ksn_Click"
18                 getItemCount="nCount"
19                 getItemID="nID"
20                 getItemLabel="nLabel" 
21                 getText="nMoren">
22               </comboBox>
23               <comboBox 
24                 id="ks_y"
25                 sizeString="9999"
26                 onChange="ksy_Click"
27                 getItemCount="yCount"
28                 getItemID="yID"
29                 getItemLabel="yLabel" 
30                 getText="yMoren">
31               </comboBox>
32             </box>
33             <box id="rxbox21"
34               boxStyle="horizontal">
35               <comboBox 
36                 id="js_n"
37                 label="结束日期:"
38                 onChange="jsn_Click"
39                 getItemCount="nCount"
40                 getItemID="nID"
41                 getItemLabel="nLabel" 
42                 getText="nMoren">
43               </comboBox>
44               <comboBox 
45                 id="js_y"
46                 sizeString="9999"
47                 onChange="jsy_Click"
48                 getItemCount="yCount"
49                 getItemID="yID"
50                 getItemLabel="yLabel" 
51                 getText="yMoren">
52               </comboBox>
53             </box>
54           </box>
55           <button id="chaxun"
56             image="chaxun"
57             label=" 开始查询 &#x0D;"
58             size="large"
59             onAction="chaxun_click"/>
60         </group>
61       </tab>
62     </tabs>
63   </ribbon>
64 </customUI>
custom ui editor for microsoft office 打开excel中写入的代码
 1 \'生成年份下拉框选项文本
 2 Public ksrq As Date, jsrq As Date
 3 Sub nCount(control As IRibbonControl, ByRef returnedVal)
 4     returnedVal = 16
 5 End Sub
 6 Sub nID(control As IRibbonControl, index As Integer, ByRef id)
 7     id = control.id & index
 8 End Sub
 9 Sub nLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
10     returnedVal = 2000 + index & ""
11 End Sub
12 
13 \'生成月份下拉框选项文本
14 Sub yCount(control As IRibbonControl, ByRef returnedVal)
15     returnedVal = 12
16 End Sub
17 Sub yID(control As IRibbonControl, index As Integer, ByRef id)
18     id = control.id & index
19 End Sub
20 Sub yLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
21     returnedVal = index + 1 & ""
22 End Sub
23 
24 
25 \'设置默认值
26 Sub nMoren(control As IRibbonControl, ByRef returnedVal)
27     returnedVal = IIf(Left(control.id, 1) = "k", "2010年", "2015年")
28 End Sub
29 Sub yMoren(control As IRibbonControl, ByRef returnedVal)
30     returnedVal = IIf(Left(control.id, 1) = "k", "1月", "12月")
31 End Sub
32 
33 
34 \'选择年月,保存到变量
35 Sub ksn_Click(control As IRibbonControl, text As String)
36     ksrq = DateSerial(Val(Left(text, 4)), Month(ksrq), 1)
37 End Sub
38 Sub ksy_Click(control As IRibbonControl, text As String)
39     ksrq = DateSerial(Year(ksrq), Val(LeftB(text, 2)), 1)
40 End Sub
41 Sub jsn_Click(control As IRibbonControl, text As String)
42     jsrq = DateSerial(Val(Left(text, 4)), Month(jsrq) + 1, 0)
43 End Sub
44 Sub jsy_Click(control As IRibbonControl, text As String)
45     jsrq = DateSerial(Year(jsrq), Val(LeftB(text, 2)) + 1, 0)
46 End Sub
47 
48 
49 \'点击查询按钮
50 Sub chaxun_click(control As IRibbonControl)
51     MsgBox "开始日期:" & Format(ksrq, "yyyy-mm-dd") & Chr(13) _
52     & "结束日期:" & Format(jsrq, "yyyy-mm-dd")
53 End Sub
vba中需要配合的代码

http://club.excelhome.net/forum.php?mod=viewthread&tid=709306&extra=page%3D1

 

以上是关于javascript中怎样添加下拉列表框的option到指定位置的主要内容,如果未能解决你的问题,请参考以下文章

如何获取下拉列表框的值

Datatable中的某行某列能不能添加一个下拉框的控件

输出选中的下拉框的值

从网页中复制的下拉框粘贴到EXCEL中,如何导出下拉框中的数据,或者网页中下拉框的数据要怎样快速地采集

关于下拉框的取值与显示问题

yii中怎样用jq得到下拉框的值并赋值给文本框