D1V1网社区 @开门芝麻网 吃饭赚钱 睡觉赚钱 做梦赚钱 http://sns.d1v1.com & http://www.KaiMenZhiMa.com/

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3426|回复: 1

Html获取select元素的真实值和显示值

[复制链接]
发表于 2013-12-26 21:59:17 | 显示全部楼层 |阅读模式 <
开门芝麻网
连劲智播AI智能自动播实景无人直播(APP免费注册下载)http://kaimenzhima.com/forum.php?mod=viewthread&tid=1
[p=164, null, left]

[p=164, null, left]HTML

[p=164, null, left]页面中,

[p=164, null, left]有时候会让我们获取当前

[p=164, null, left]select

[p=164, null, left]元素中所选中的那个值和他的

[p=164, null, left]显示值。下面是一个例子:


[p=164, null, left]<tr>

[p=164, null, left]     <th scope="row" width="15%" nowrap >*

[p=164, null, left]目标字段

[p=164, null, left]</th>

[p=164, null, left]     <td><select

[p=164, null, left]name="idMbzd"

[p=164, null, left]style="width:25%"

[p=164, null, left]onchang=”on_idmbzd_change();”>



[p=164, null, left]<option value=”1”>eg1</option>


[p=164, null, left]           <option value

[p=164, null, left]=”2”>eg2</option>



[p=164, null, left]<option value=”3”>eg2</option>


[p=164, null, left]         </select>

[p=164, null, left]     </td>

[p=164, null, left]</tr>

[p=164, null, left]<script>

[p=164, null, left]function on_idmbzd_change(){

[p=164, null, left]       var sel_obj = document.getElementByIdx("idMbzd ");

[p=164, null, left]       var index = sel_obj.selectedIndex;

[p=164, null, left]       alert(sel_obj.options[index].value);

[p=164, null, left]       alert(sel_obj.options[index].text);  

[p=164, null, left]}

[p=164, null, left]</script>

[p=164, null, left]在这里,关键用到

[p=164, null, left]select

[p=164, null, left]对象的

[p=164, null, left]selectedIndex

[p=164, null, left]属性。表示被选中那个元素的

[p=164, null, left]索引,从

[p=164, null, left]0

[p=164, null, left]开始。


[p=164, null, left]当然也可以遍历

[p=164, null, left]select

[p=164, null, left]元素的所有值。如下:


[p=164, null, left]<script>

[p=164, null, left]var len  = sel_obj.options.length;


开门芝麻网
部分内容由网友发布或收集于互联网,如有侵权,请联系QQ/微信76815288,第一时间删除!(开门芝麻网 sns.d1v1.com)
回复

使用道具 举报

 楼主| 发表于 2013-12-26 22:04:04 | 显示全部楼层 <
在HTML页面中,有时候会让我们获取当前select元素中所选中的那个值和他的显示值。下面是一个例子:  <tr>  
     <th scope="row" width="15%" nowrap >*目标字段</th>
      <td><select name="idMbzd" style="width:25%"  onchang=”on_idmbzd_change();”>            <option value=”1”>eg1</option>
           <option value=”2”>eg2</option>
           <option value=”3”>eg2</option>          </select>
     </td> </tr>
<script>  function on_idmbzd_change(){
      var sel_obj = document.getElementByIdx("idMbzd ");  
      var index = sel_obj.selectedIndex;
      alert(sel_obj.options[index].value);  
      alert(sel_obj.options[index].text);  }  </script>

在这里,关键用到select对象的selectedIndex属性。表示被选中那个元素的索引,从0开始。  当然也可以遍历select元素的所有值。如下:
<script>  var len  = sel_obj.options.length;

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
400-888-xxxx

微信扫一扫,私享最新原创实用干货

QQ|申请友链|Archiver|手机版|小黑屋|D1V1网社区 @开门芝麻网 ( 沪ICP备15050032号-2 )

GMT+8, 2024-5-5 12:57 , Processed in 0.191089 second(s), 36 queries .

Powered by Discuz! X3.4 Designed by www.D1V1.cn

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表