来源:蜘蛛抓取(WebSpider)
时间:2016-02-17 12:45
标签:
javascript select 值
javascript获取select的值全解-js教程-网页制作-壹聚教程网javascript获取select的值全解下面我总结了大量的获取select的值的希望此例子对各位同学会带来帮助哦,文章有点长但都是重点内容。
获取显示的汉字
document.getElementById(&bigclass&).options[window.document.getElementById(&bigclass&).edIndex].text
获取中的id
window.document.getElementById(&bigclass&).value
获取select组分配的索引id
window.document.getElementById(&bigclass&).selectedIndex
&select name=&bigclass& id=&bigclass& onChange=&:updatePage2();&&
&option value=&& selected=&selected&&ajax实验&/option&
&option value=&4&P&我适宜市哈&/option&
document.getElementById(&bigclass&).options[window.document.getElementById(&bigclass&).selectedIndex].text
的结果是:我适宜市哈
window.document.getElementById(&bigclass&).value
的结果是:4
window.document.getElementById(&bigclass&).selectedIndex
的结果是:1
一、新增一个option
&&& var sel=document.getElementById(&select的id&);
&&& var op=document.createElement(&option&);
&&&& op.value=值;
&&&& op.text=显示文本;
&&&& sel.add(op);
二、删除一个option
&&& var sel=document.getElementById(&typelist&);
if(sel.selectedIndex==-1)
&& alert(&请选中要删除的项!&);
for(var i=0;i&sel.options.i++){
if(sel.options[i].selected){
sel.options.remove(i);
三、清空select的所有option
&& var citySel=document.getElementById(&select的id&);
&& citySel.options.length=0;
四、获得选中项的值
&&& var citySel=document.getElementById(&select的id&);
&&& var selectedValue=citySel.
五、获得当前选中项的索引
&&& var selectedIndex=document.all.objSelect.selectedI
六、设置select的当前选中项
&&& 方法1(单个select): document.getElementById(&products_type_id&).selectedIndex=1;
&&& 方法2(级联select如省市级联):
&&&& var province_sel=document.getElementById(&province&);//获得省select
var city_sel=document.getElementById(&city&);//获得市select
for(var i=0;i&province_sel.options.i++){
if(province_sel.options[i].value==&从获取的省的值&){
&&& province_sel.options[i].selected=
initCity(&从数据库获取的省的值&);//初始化市select
for(var i=0;i&city_sel.options.i++){
if(city_sel.options[i].value==&${city}&){
&&& city_sel.options[i].selected=
七、创建select动态设置选中项
&& var sel=document.getElementById(&other_state&);
&&&& var sel_val=document.getElementById(&other_media_id&).innerHTML;
for(var obj in data){
var id=data[obj][&other_media_id&];&
var name=data[obj][&other_media_name&];
var op=document.createElement(&option&);
op.setAttribute(&value&,id);
op.appendChild(document.createTextNode(name));&&&&
&&&&&&&&&& if(id==sel_val){
op.setAttribute(&selected&,&true&);
&&&&&&&&&&& sel.appendChild(op);
1、向Select里添加Option
function fnAddItem(text,value)
&&&&&&&&&&& var selTarget = document.getElementById(&selID&);
&&&&&&&&&&& selTarget.Add(new Option(&text&,&value&));
2、删除Select里的Option
function fnRemoveItem()
&&&&&&&&&&& var selTarget = document.getElementById(&selID&);
&&&&&&&&&&& if(selTarget.selectedIndex & -1)
&&&&&&&&&&& {//说明选中
&&&&&&&&&&&&&&& for(var i=0;i&selTarget.options.i++)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& if(selTarget.options[i].selected)
&&&&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&&&&&& selTarget.remove(i);
&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&& i = i & 1;//注意这一行
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
3、移动Select里的Option到另一个Select中
&&&&&&& function fnMove(fromSelectID,toSelectID)
&&&&&&&&&&& var from = document.getElementById(fromSelectID);
&&&&&&&&&&& var to = document.getElementById(toSelectID);
&&&&&&&&&&
&&&&&&&&&&& for(var i=0;i&from.options.i++)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& if(from.options[i].selected)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& to.appendChild(from.options[i]);
&&&&&&&&&&&&&&&&&&& i = i & 1;
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&& if 里的代码也可用下面几句代码代替
var op = from.options[i];
to.options.add(new Option(op.text, op.value));
from.remove(i);
4、Select里Option的上下移动
&&&&&&& function fnUp()
&&&&&&& {&
&&&&&&&&&&& var sel = document.getElementById(&selID&);
&&&&&&&&&&& for(var i=1; i & sel. i++)
&&&&&&&&&&& {//最上面的一个不需要移动,所以直接从i=1开始
&&&&&&&&&&&&&&& if(sel.options[i].selected)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& if(!sel.options.item(i-1).selected)
&&&&&&&&&&&&&&&&&&& {//上面的一项没选中,上下交换
&&&&&&&&&&&&&&&&&&&&&&&&& var selText = sel.options[i].
&&&&&&&&&&&&&&&&&&&&&&&&& var selValue = sel.options[i].
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options[i].text = sel.options[i-1].
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options[i].value = sel.options[i-1].
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options[i].selected =
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options[i-1].text = selT
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options[i-1].value = selV
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options[i-1].selected=
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
在进行上下两项互换时,也可以使用以下代码,但是效率很低,因为每一次的Dom操作都将导致整个页面的重新布局,所以不如直接修改元素的属性值。
&&&&&&&&&&&&&&&&&&&&&&& var oOption = sel.options[i]
&&&&&&&&&&&&&&&&&&&&&&& var oPrevOption = sel.options[i-1]
&&&&&&&&&&&&&&&&&&&&&&& sel.insertBefore(oOption,oPrevOption);
向下移动同理
function fnDown()
&&&&&&&&&&& var sel = fnGetTarget(&selLeftOrRight&);
&&&&&&&&&&& for(var i=sel.length -2; i &= 0; i&)
&&&&&&&&&&& {//向下移动,最后一个不需要处理,所以直接从倒数第二个开始
&&&&&&&&&&&&&&& if(sel.options.item(i).selected)
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& if(!sel.options.item(i+1).selected)
&&&&&&&&&&&&&&&&&&& {//下面的Option没选中,上下互换
&&&&&&&&&&&&&&&&&&&&&&&&& var selText = sel.options.item(i).
&&&&&&&&&&&&&&&&&&&&&&&&& var selValue = sel.options.item(i).
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options.item(i).text = sel.options.item(i+1).
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options.item(i).value = sel.options.item(i+1).
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options.item(i).selected =
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options.item(i+1).text = selT
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options.item(i+1).value = selV
&&&&&&&&&&&&&&&&&&&&&&&&& sel.options.item(i+1).selected=
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
5、Select里Option的排序
这里借助Array对象的sort方法进行操作,sort方法接受一个function参数,可以在这个function里定义排序时使用的算法逻辑。
array.sort([compareFunction]) 里compareFunction接受两个参数(p1,p2),sort操作进行时,array对象会每次传两个值进去,进行比较;compareFunciton必须返回一个整数值:当返回值&0时,p1会排在p2后面;返回值&0时,p1会排在p2前面;返回值=0时,不进行操作。
function fnCompare(a,b)
&&&&&&&&&&& if (a & b)
&&&&&&&&&&&&&&& return -1;
&&&&&&&&&&& if (a & b)
&&&&&&&&&&&&&&& return 1;
&&&&&&&&&&& return 0;
var arr = new Array();
//add some value into arr
arr.sort(fnCompare);
//这里sort的操作结果就是arr里的项按由小到大的升序排序
//如果把fnCompare里改为
//if (a & b)
// return 1;
//if (a & b)
// return -1;
//return 0;
//则sort的结果是降序排列
好,下面就是对Select里Option的排序
//因为排序可以按Option的Value排序,也可以按Text排序,这里只演示按Value排序
function sortItem()
&&& var sel = document.getElementById(&selID&);
&&& var selLength = sel.options.
&&& var arr = new Array();
&&& var arrL
&&& //将所有Option放入array
&&& for(var i=0;i&selLi++)
&&&&&&& arr[i] = sel.options[i];
&&& arrLength = arr.
&&& arr.sort(fnSortByValue);//排序
&&& //先将原先的Option删除
&&& while(selLength&)
&&&&&&& sel.options[selLength] =
&&& //将经过排序的Option放回Select中
&&& for(i=0;i&arrLi++)
&&&&&&& sel.add(new Option(arr[i].text,arr[i].value));
function fnSortByValue(a,b)
&&& var aComp = a.value.toString();
&&& var bComp = b.value.toString();
&&& if (aComp & bComp)
&&&&&&& return -1;
&&& if (aComp & bComp)
&&&&&&& return 1;
&&& return 0;
排序时还可以有更多选项,比如将value值看做Integer或是String进行排序,得到的结果是不一样的。篇幅限制,不在多做介绍。
上一页: &&&&&下一页:相关内容
秀美甲告白小人编辑推荐
电脑壁纸相关专题用javascript生成一个注册页面,要求用户名和密码不能为空,密码和确认密码一样,年龄在0到100之间,急_百度知道
用javascript生成一个注册页面,要求用户名和密码不能为空,密码和确认密码一样,年龄在0到100之间,急
/form name=&&font color=red>.form1:& name=&/div3& onBlur=& value=".学历是;&
var password =meta http-equiv=&checkbox14& name=&&<.innerHTML = &
&其他".text4;硕士&;":inline&div1&".form1;
return false.innerHTML = &
&学历.innerHTML = ",手机号码是;篮球&
alert(".text1;
¢er&/, 0);font>,爱好;form>.form1;option&
javascript&div5&/.text5;
font color=red&text":
&/.innerHTML = &
&text4&head&&font color=red>.form1;td&div align=& charset=utf-8".form1;&
&&td&div&/&
/":inline",@符号必须在;&&div&checkbox13&姓名不能为空.innerHTML = &tr&Content-Type& 12) {
&tr>:inline&option>.innerHTML = &&script language=&password&font color=red>.focus().getElementById(&.select();
function checkPhone(){
var div =text&div2&
<.innerHTML = &td&
&td&本科&/.indexOf(&
&&div id=&.focus()!= repass) {
@".length == 11) {
& style=&td&&
td&;&input id=&+e+&/div align=&td>.indexOf(& style=&
;重置&&;font&tr&font color=red&&check()":&
option value=&>.form1;div4".select();
&本科&&音乐&/div align=& name=&display.form1;
& 4 || name1;option&td&div id=&
//input type=&中专&select>.innerHTML = ".
&/.innerHTML = && type=&td&
if (.innerHTML = &//&&div&&>.form1;check()".select();&
/div id=".length &&
&电子邮件地址;
<.select();
div.text5;
<.text2; name=&input id=&tr&
&body&div id=&
var tt = sw1 - sw.getElementById(&&
&博士&& value=&//&form1&/text9&
<,必须包含@符号;/
function check(){
if (checkname() && checkpassword() && checkrepassword() && checkEmail()&&checkPhone()) {
var a = document.indexOf(&/ style=".focus();¢er& /手机号码只能为11个字符;
<.之前&font>.text4; onBlur=&& value=&
<.form1:
<.select();
<.length &
& value=".);
&博士&&td&tr align=&<!<, 0);&&) {
div.text3;确认密码,必须包含;& name=&font color=red¢er& name=".select();text/center&
var sw =div align=&input id=&td&&
其他&input type=&
if (tt == 1) {
input type=&&font color=red&&tr&/
if (phone.form1;
&>.select().&text&
&&title>.getElementById(&/&/
&用户名;td&
var e =script¢er&&option>.getElementById(".form1;
function checkEmail(){
var div =font&td&密码;>.text1.form1;/
&checkbox12&/
<.focus();&font&/.length & onBlur=&
&输入密码和确认密码长度不一致<.form1.font>.text6;
function checkpassword(){
var div =;大专&>!&);option value=&
if (password == &<.text5.form1;>!&/:
<.select();&手机号不能为空;电子邮件格式不正确;tr& type=".length == 0) {
;input type=&font&right&硕士&).text2;/高中&
&div align=&&font color=red& 4 ||
&& name=&return check()&tr>.innerHTML = ":&初中&body&高中&left".手机号码;div4&font&/>.text2;option value=&//.text4;&
<.text4;input id=&
style=&&/电子邮件格式不正确.length &//.".form1;td>.text3;div align=& onBlur=& type="!&>.focus();&;
if (".
if (phone == &;font color=red¢er&
&td&check()&&font color=red&tr align=&div&td&
&option value=&/;reset&
&电子邮件格式不正确;
right&/td¢er& align="!&
&;font color=red&div>.text4.form1;
&div align=&;欢迎进入注册页面<.innerHTML = ".innerHTML = &&".innerHTML = &
&check()"!&
var email =div1&right& style=&
var phone =tr&
&td align=&>.innerHTML = ".form1;//.text2;>.form1;大专&密码不能为空;
&邮件格式不对;text5&
function checkrepassword(){
var div =>.text5;
if (font>.innerHTML = &tr&
name=&title&table border=&.text4;
&font>。@和;&option value=&+a+&text3&text1& 16) {
var b = document:", 0) == -1) {
text8&td&>.text4;input id=&
password&/
&option&) {
checkbox&tr&1",密码;right&兴趣爱好;&
<:&select name=".getElementById(&/
var password =/+c+&
/.form1;submit¢er&right&div&
<.form1;&&
var name1 =font color=red& onBlur=&
function checkname(){
var div =/!&td>!&
if (email!&初中&text2&div&option&&checkbox&&
<.value.form1;
&/;div>.text3;.indexOf(&
&td align=&+b+":
&/.innerHTML = &/.text1;密码长度4-12位& type=&tr&
value=&电子邮件不能为空&div align=".form1;td align=&
var d = document,邮件;;;
);用户名;/check()&
&option value=&html&div&//tr>.text2;
var repass =
提交&option&
&; name=&>.form1; name=& value=&
if (repass == &&td align=&text1&input type=&密码不能为空;div&div&text4&<, 0) == -1) {
if (name1;
if (name1 == &text2&td& type=&tr&
/姓名输入的长度4-16个字符;
&text3".&font color=red&
var sw1 =option value=&td&@&td&中专&tr align=&
/html&/.value.&
text5&&checkbox&td&&;/ name=&;
return false:inline&/.font>.符号;
onSubmit=&font&checkbox".text1;;//:&div2&) {
head>.form1;&
if (&input type=&&div&足球".不可以挨着; content=&
div>.innerHTML = &&/;div3"给你个类似的话 自己改下就好<:&
&table>:&
来自团队:
其他类似问题
为您推荐:
其他1条回答
专门为你写的,够意思吧。
&!DOCTYPE html PUBLIC &-//W3C//DTD XHTML 1.0 Transitional//EN& &http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&&
&html xmlns=&http://www.w3.org/1999/xhtml&&
&style type=&text/css&&
li{line-height:30}
.styleinput{margin-left:80width:150}
.styleinput2{margin-left:16width:150}
&meta http-equiv=&Content-Type& content=&text/ charset=utf-8& /&
&link href=&style/form.css& rel=&stylesheet& type=&text/css& /&
&title&无标题文档&/title&
&script language=&JavaScript&&
function checkpost()
if(form1.name.value==&&)
alert(&请输入用户名&);
form1.name.focus();
return fal...
javascript的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁一个select没有用onchanjavascript:void(0);ge事件但是也能执行某一个JS这么实现的?_百度知道
一个select没有用onchanjavascript:void(0);ge事件但是也能执行某一个JS这么实现的?
我有更好的答案
啊?????
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁