js操作select相关方法

上一篇 / 下一篇  2008-08-25 12:25:01 / 个人分类:.NET

《js操作select相关方法》

1.判断select选项中 是否存在value="paravalue"的item51Testing软件测试网(q`] eZ D
2.向select选项中 加入一个item
_+QJ:kv03.从select选项中 删除一个item
!Y \|AX04.修改select选项中 value="paravalue"的text为"paratext"51Testing软件测试网:n:Qb!_ ?4MB m
5.设置select中text="paratext"的第一个item为选中51Testing软件测试网L[XSk q+\
6.设置select中value="paravalue"的item为选中
G`%p-z7e f)^p2V'A07.得到select的当前选中项的value51Testing软件测试网^2y\#i&[y4sc-\+?r
8.得到select的当前选中项的text
*Y4M)B/AE"e%fec09.得到select的当前选中项的index
:q M-?m}\"E.nG010.清空select的项51Testing软件测试网#`1xSEyc&P7p V
-------------------------------------------51Testing软件测试网{S"~6j~H/N9K,m[_ R
//1.判断select选项中 是否存在value="paravalue"的item51Testing软件测试网w5e6c7p|*W |
function jsselectisexititem(objselect,objitemvalue)
8DD:bW6c)WBO)]8Z0{
&~!c6Kh'IL?5@0    var isexit = false;
~"W,r0i,{3i+IT J*f0    for(var i=0;i<objselect.options.length;i++)
D3^D`M+v k8Jw0    {51Testing软件测试网 e7s&E$H.~%t
  if(objselect.options[i].value == objitemvalue)
?1{p _/}p0  {
e!wT:MK6m+_0      isexit = true;51Testing软件测试网f&{6PU6a+fUP
      break;51Testing软件测试网)U-Do)?f^|
  }51Testing软件测试网,G/f0e;Y/QW|
    }     
YzY JG_wa0    return isexit;51Testing软件测试网V V.zE*U
} 51Testing软件测试网8R;P!Rd(n5g

//2.向select选项中 加入一个item
7V x;{#JA8x0function jsadditemtoselect(objselect,objitemtext,objitemvalue)
W/O#],X#D9W0{
.V/f5d ['A:U'h'p0    //判断是否存在
Sj+G,yeR+p|-m:}0    if(jsselectisexititem(objselect,objitemvalue))
u;R,tI,LJ-Bm~~0    {
-m@co*v kC5L0  alert("该item的value值已经存在");
#q$i'I}OX@0    }
2D0b%e"BHf0    else
rX"Q_@4|0    {
~l;Icy(@&pg2i]0  var varitem = new option(objitemtext,objitemvalue);
$p0o V1H0d S:q+H0//      objselect.options[objselect.options.length] = varitem;51Testing软件测试网mJJ }~-}
  objselect.options.add(varitem);
9o_6_ j{-w0  alert("成功加入");51Testing软件测试网5c!E0KBqli!I
    }    51Testing软件测试网z C)o(~N%U(]*G{v
}51Testing软件测试网+R(G!Z'gr

//3.从select选项中 删除一个item
b0\\p(oF(v8rL)\0function jsremoveitemfromselect(objselect,objitemvalue)
d.`l ?IU;S8SN0{51Testing软件测试网f/q(|"?4cV.oG\-u
    //判断是否存在51Testing软件测试网"P~J#vJz c
    if(jsselectisexititem(objselect,objitemvalue))
uts!|v \3n,J0    {
j4r;R6^)MKg_$E0  for(var i=0;i<objselect.options.length;i++)51Testing软件测试网3gC@4d8T:VPJ2fm
  {
7@~c*e wu]}0      if(objselect.options[i].value == objitemvalue)
(`D)iSMqN.v0      {
_!ch`6K3l.F0    objselect.options.remove(i);51Testing软件测试网{Q4x!m ^s:sl2r
    break;51Testing软件测试网b*}HOK,K
      }
'MVjb)k\4r.i0  } 
9HN^Ta dNj0  alert("成功删除");      51Testing软件测试网 i)L\,p+HDs:o8N
    }51Testing软件测试网6QyF|;dQb3t
    else
0d5ps$D ^L'Q\0    {
'N#^5EM+_0  alert("该select中 不存在该项");51Testing软件测试网7_htI?p%_(b F
    }    51Testing软件测试网q&@;QE*RD ]
}51Testing软件测试网\SGj:e1Mp~i

//4.修改select选项中 value="paravalue"的text为"paratext"
)P.^sX"jCX |0function jsupdateitemtoselect(objselect,objitemtext,objitemvalue)
0U,{0h1vwC;?l0e1s(Y!w(j0{
m~i:Hn%F"d)DA2J0    //判断是否存在
8m:j|`C0    if(jsselectisexititem(objselect,objitemvalue))51Testing软件测试网4P+] {.nDZ5g"D
    {
pN+WU7o'C*k J0  for(var i=0;i<objselect.options.length;i++)
7t_v!E)rn%C0  {
)n#i/p.jb6sv!_0      if(objselect.options[i].value == objitemvalue)51Testing软件测试网]"j%Y"`+{
      {
"f0Pg p1O4Ged%H0    objselect.options[i].text = objitemtext;51Testing软件测试网6?&VU r?m+S
    break;
6j*W5vM}M)V0      }51Testing软件测试网 @3O9lTc%P5D
  }  51Testing软件测试网l9vB/C)Jl3Zv
  alert("成功修改");      51Testing软件测试网:P0Ua:a{o
    }
2ispZH e z$H Q7r,c0    else51Testing软件测试网l$nc0z,AZ]'F
    {
Fa'n(FQ d$@0  alert("该select中 不存在该项");
U Z oWE P$x \2AXuJ0    }    51Testing软件测试网mu]2D o%\^ t8Mh
}
L(^,V.h3e*en1w(H&i0 
t'OO%x}Nex0//5.设置select中text="paratext"的第一个item为选中51Testing软件测试网:@+o kz:?*F E
function jsselectitembyvalue(objselect,objitemtext)51Testing软件测试网7zP S%R,N Px.D
{   
"[-|R7u:sMcIs&[N0    //判断是否存在51Testing软件测试网\a/c7jiNW6|$fX
    var isexit = false;51Testing软件测试网#{ W+NgiLF
    for(var i=0;i<objselect.options.length;i++)
*m4G M7U5{_b']*CcD0P3@#}0    {51Testing软件测试网$@-fJIBG
  if(objselect.options[i].text == objitemtext)
3dfbYr {0  {
:e t2^q{9}W#U.wc W0      objselect.options[i].selected = true;
!pC0~;x{t'C2y0      isexit = true;51Testing软件测试网.M/]P*{#bS
      break;
PUy2f+iz0  }
G [ Y_B/E@d9zx N0    }     
"\x `B){#eV.vf*P0    //show出结果51Testing软件测试网8M CXZ d1`
    if(isexit)
8MU:bh s"gE0    { 51Testing软件测试网SYUQ5j2JLp
  alert("成功选中");     
"d/mw1y_Tn/d;O\0    }
+]_ B;V U9w0    else51Testing软件测试网^ r9b M%]#t%G-[Y`
    {
EGb{n0  alert("该select中 不存在该项");51Testing软件测试网a%VQW4EoMsU-[
    }   
Y3_ `$s{N0}51Testing软件测试网2rML ?:x3K,P1w

//6.设置select中value="paravalue"的item为选中
;]iI2F#q*gOYO0//document.all.objselect.value = objitemvalue;

)LHxAA.c3P3XY0

//7.得到select的当前选中项的value51Testing软件测试网rL K6J W1uoe2W
//var currselectvalue = document.all.objselect.value;

LI/g1j_.H.G0

//8.得到select的当前选中项的text51Testing软件测试网 i\:Xpd,C8DZ
//var currselecttext = document.all.objselect.options[document.all.objselect.selectedindex].text;

sY0Ed1ua\4q0

//9.得到select的当前选中项的index
"~(s0@R2_6X'?p0//var currselectindex = document.all.objselect.selectedindex;51Testing软件测试网`2B$q8Td7s R+Eq_7t

//10.清空select的项51Testing软件测试网/I$M I7z|*u|
// document.all.objselect.options.length = 0;51Testing软件测试网i(H Cw'b)@ AJ3SY


TAG:

 

评分:0

我来说两句

Open Toolbar