日期:2014-05-16  浏览次数:20414 次

单选按钮怎么不显示id 而是undefined
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script>
<!--
   
    function getVById(){

var tt=this.id;
alert(tt);
}
    
-->

</script>
</head>
<body>
    <input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
    <input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
   
    
</body>
<html>
HTML this?

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

直接在function里面获取的this是Window对象
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript">

   window.onload= function(){
  var atr = document.getElementsByTagName('input');
  //alert('0');
 for(i=0;i<atr.length;i++)
{

    atr[i].onclick=function (){

var tt=this.id;
alert(tt);
}
    
}

</script>
</head>
<body>
    <input type="radio" id="test11" name="test11" value="1"onclick="getVById()" />测试1
    <input type="radio" id="test12" name="test11" value="2" onclick="getVById()"/>测试2
   
    
</body>
<html>


这里我还是直接用this  但又行了  这是什么原因啊  也是直接在函数里面调用的