猿问

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

<!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一个样

麻烦谁帮忙解决下

LEATH
浏览 384回答 1
1回答

临摹微笑

你的option中并没有写value,所以IE7读取不到,但是其他浏览器做过优化,不写value时默认把text当作value,所以两种方案: 一、给option加上value属性 二、用var valselect=objselect.options[indexselect].text;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答