首页 新闻 赞助 找找看

IE7下js读取select选取项值的问题

0
悬赏园豆:20 [已解决问题] 解决于 2012-07-05 08:33

<!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">

  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="xxx.js"></script>
  </head>
  <body>
    <select id="select">
      <option>请选择项</option>
      <option>dasfasdf</option>
      <option>123</option>

    </select>
  </body>
</html>

 

function $(id){
  return document.getElementById(id);
}

window.onload = function(){

  $('select').options[0].selected=true;
  $('select').onchange=function(){
    var objselect=$('select');
    var indexselect=objselect.selectedIndex;
    var valselect=objselect.options[indexselect].value;
    alert(valselect);

  }
}

 

在IE8、9、10下都能正常运行,chrome下也没问题,就IE7不行,读取出来的数据为空,IE6未尝试,不过估计跟IE7一个样

麻烦谁帮忙解决下

trirocky的主页 trirocky | 初学一级 | 园豆:126
提问于:2012-07-05 06:30
< >
分享
最佳答案
0

你的option中并没有写value,所以IE7读取不到,但是其他浏览器做过优化,不写value时默认把text当作value,所以两种方案:

一、给option加上value属性

二、用var valselect=objselect.options[indexselect].text;

收获园豆:20
artwl | 专家六级 |园豆:16736 | 2012-07-05 08:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册