js操作select相关方法

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

《js操作select相关方法》

1.判断select选项中 是否存在value="paravalue"的item
+rY$Z D\*ed6l$?02.向select选项中 加入一个item
"Zlh-VQ;`03.从select选项中 删除一个item
Q8Ra,D Etv5Rq\B04.修改select选项中 value="paravalue"的text为"paratext"51Testing软件测试网7x(Z%tn4g!^0S}w
5.设置select中text="paratext"的第一个item为选中51Testing软件测试网z;G {|1ZZ%`3w
6.设置select中value="paravalue"的item为选中
o"?r9^].re]07.得到select的当前选中项的value
r$fh \ V'T@;x\08.得到select的当前选中项的text51Testing软件测试网mR I1OB Ph,su Ah
9.得到select的当前选中项的index
m&?OT&j/x-T @E010.清空select的项
{#av/hf;~ ZC:t0-------------------------------------------
!^9XL-wo6l0//1.判断select选项中 是否存在value="paravalue"的item51Testing软件测试网P _,okS
function jsselectisexititem(objselect,objitemvalue)51Testing软件测试网m ^4ExX#fP3d Gf
{51Testing软件测试网0A'~s+C~/Wm
    var isexit = false;
4\9_-GH.W{%O[0    for(var i=0;i<objselect.options.length;i++)
UTwB2J[.k!^bYf/b0    {51Testing软件测试网I4bxkv'{
  if(objselect.options[i].value == objitemvalue)51Testing软件测试网w3u3vHO+GK
  {
oCz'f#q&e2d;T2t0      isexit = true;51Testing软件测试网'ma1V(w9r@g0f)W
      break;51Testing软件测试网4m OI1_)S["f*l
  }
T(kMjuvd0    }      51Testing软件测试网/YO l+SH9]
    return isexit;
IE6gz6?*u0}

H!~&I:AFyE8f!nA0

//2.向select选项中 加入一个item
7AJhw vx^P8t!N eN0function jsadditemtoselect(objselect,objitemtext,objitemvalue)
H@@)ILGj0{51Testing软件测试网^@cK/b}7E`
    //判断是否存在
J;al8oz oB x&~Cn0    if(jsselectisexititem(objselect,objitemvalue))51Testing软件测试网q;t!XZ#S'Z
    {51Testing软件测试网S ~ |F:O9fK
  alert("该item的value值已经存在");
vXgb+D0    }
X.~on0t0    else
!l Gn h7vxJ m.m3Wj0    {51Testing软件测试网RP)JM.{"l}
  var varitem = new option(objitemtext,objitemvalue);
mCN:K6D I1N0//      objselect.options[objselect.options.length] = varitem;51Testing软件测试网 yP9o l.l
  objselect.options.add(varitem);51Testing软件测试网GBa DQ(V7P$s6MF
  alert("成功加入");51Testing软件测试网8P-l~+t g$_
    }    51Testing软件测试网5r)I1Y9Ss0WnR
}51Testing软件测试网h.Ahve$j,s8M J

//3.从select选项中 删除一个item
;rsRh)|U'F0function jsremoveitemfromselect(objselect,objitemvalue)
-I!C0bmj OiO0{
)@{k!{k0    //判断是否存在51Testing软件测试网-_SRV[*}@Y
    if(jsselectisexititem(objselect,objitemvalue))
}sA5gU8q0MP~0    {51Testing软件测试网F5f mh-`S
  for(var i=0;i<objselect.options.length;i++)
,A(^o`W3F f0  {
5H3|D[j0      if(objselect.options[i].value == objitemvalue)
K$c;s.|T6eJ(|'g0      {
u w9SH},CA$d0    objselect.options.remove(i);51Testing软件测试网(d+SC8[1y&Z6m2@ {
    break;
l(G#w:C1j E3J:u2D:f0      }
r:|of_0  }  51Testing软件测试网.F \u koXz
  alert("成功删除");     
#@z,asu'| bG0    }51Testing软件测试网l+^K|\ f
    else
l-l:TS[U:LxO0    {51Testing软件测试网*O|B2sM
  alert("该select中 不存在该项");
$f A-@HSUl8w6V0    }   
9x8p+SHNM0}51Testing软件测试网S7a/[3F-Y S7~

//4.修改select选项中 value="paravalue"的text为"paratext"51Testing软件测试网 G6q"e;@&D#G1Q
function jsupdateitemtoselect(objselect,objitemtext,objitemvalue)
6KQ8c+E1@Z0{51Testing软件测试网ugq P'{
    //判断是否存在
$iE2d^`mr0    if(jsselectisexititem(objselect,objitemvalue))51Testing软件测试网9|~\ [qj9{
    {
cv.`)O6V3\0  for(var i=0;i<objselect.options.length;i++)51Testing软件测试网$Sm1D@@8]S ]
  {
0y&e9^.aj%Vu z0      if(objselect.options[i].value == objitemvalue)51Testing软件测试网8R Z4o"R#P
      {
O"wj t!_c(KBz w-S0    objselect.options[i].text = objitemtext;51Testing软件测试网&P2c)[#\v&vyGI:x
    break;
p;_7Q(v~0      }
5b7Z N$ej@~t0  }  51Testing软件测试网swXXn2C
  alert("成功修改");     
w2l"H(^-j"H_,o%v(\G0    }
V Z(zk`AV*y6l(@:`6R0    else51Testing软件测试网I-w/@#{8F_;t
    {
)Z@}$o`.X)A2I;n0  alert("该select中 不存在该项");
N8U vn)u OIVa3H0    }   
-FD1g_-y@h0}51Testing软件测试网H2rY2c4C|[Ai
 
4p6e+?i4hQ3F P0//5.设置select中text="paratext"的第一个item为选中51Testing软件测试网%e]t:y8F,pn{.KV%C
function jsselectitembyvalue(objselect,objitemtext)
HvM a3n sv?l6U0{   
kWGyslG#VN0    //判断是否存在51Testing软件测试网#KE%P(N1hVu)H%|
    var isexit = false;51Testing软件测试网IRi~t D
    for(var i=0;i<objselect.options.length;i++)51Testing软件测试网l2iQ]F6z
    {
4}+a @ v+L7l%uY0  if(objselect.options[i].text == objitemtext)51Testing软件测试网+J"l)yzW
  {51Testing软件测试网rK&X.Iu
      objselect.options[i].selected = true;
1yH;u l"uw0E}%RL~0      isexit = true;
i Gu Z?&o0      break;
K,} RwCY0ry0  }51Testing软件测试网U3_:\_8e U(k"q
    }      51Testing软件测试网.A `2Qs&u2\K u
    //show出结果51Testing软件测试网9FWs {]%c7Hdb
    if(isexit)51Testing软件测试网 xDGgzc
    {
\Q Y/a1Jx&t0  alert("成功选中");     
3Y Hj6k(Y7f0    }51Testing软件测试网 W;n0dH(~H_XB[
    else51Testing软件测试网YIt`k;H1H P
    {51Testing软件测试网a0l*B%PlQ:`
  alert("该select中 不存在该项");51Testing软件测试网vtE8\v%N
    }    51Testing软件测试网)`0f(TW7j _t
}51Testing软件测试网K _ sY$M|!o/lj w

//6.设置select中value="paravalue"的item为选中51Testing软件测试网*C$g#L!p_!B&`:RS
//document.all.objselect.value = objitemvalue;51Testing软件测试网GQEJ!OD

//7.得到select的当前选中项的value
EM5}s7y0//var currselectvalue = document.all.objselect.value;

$?U uJ e-p[0

//8.得到select的当前选中项的text51Testing软件测试网y1l r"iU5p
//var currselecttext = document.all.objselect.options[document.all.objselect.selectedindex].text;51Testing软件测试网a{6M;c.k,p

//9.得到select的当前选中项的index51Testing软件测试网7uy!M`(G
//var currselectindex = document.all.objselect.selectedindex;

(M4g?Rqd0

//10.清空select的项
:O&jH|,fwz)n%D&S0// document.all.objselect.options.length = 0;51Testing软件测试网;CO.s MD.Y


TAG:

 

评分:0

我来说两句

日历

« 2024-03-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 32373
  • 日志数: 39
  • 图片数: 2
  • 建立时间: 2008-04-03
  • 更新时间: 2012-02-03

RSS订阅

Open Toolbar