<!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" />
<title>js测试</title>
<script src="jquery-1.3.2.js"></script>
<script>
$(document).ready(function(){});
</script>
<script>
function test(e,id){
   var temp_id = id;
   alert(id)
   var temp = document.form1.txt1.value;//txt1非变量
   alert("非变量的值为:"+temp)
   var temp1 = document.form1.temp_id.value;//这里得到的id为txt1会报错怎么回事? 
   alert(temp1)
}
</script>
</head>
<body>
<form id="form1" name="form1">
    <input id="txt1" name="text1" onkeydown="test(event,$(this).attr('id'));" /><br />
    <input id="txt2" name="text2" onkeydown="test(event,$(this).attr('id'))"/>
</form>
</body>
</html>

解决方案 »

  1.   

    var temp_id = id;
    这个不是吗?楼上的请你看清楚再说
      

  2.   

    var temp1 = document.form1.temp_id.value;//这里得到的id为txt1会报错怎么回事? 
    temp_id要用temp_name
    form元素后只能跟name
      

  3.   

    document.getElementById(temp_id).value;
      

  4.   

    大家好,我的var temp = document.form1.txt1.value;//如果form1 和txt1的位置都是变量怎么办?
      

  5.   


    为什么要这么复杂?不是temp1=temp_id就可以了吗?
    不明白……
      

  6.   

    <script>
    function test(e,id,formId){
      var tempObj = eval(formId+"."+id);//直接拼出"form1.txt1"  alert(id);
      var temp = document.form1.txt1.value;
      alert("非变量的值为:"+temp);
      var temp1 = tempObj.value;
      alert(temp1);
    }
    </script>
    <form id="form1" name="form1">
        <input id="txt1" name="text1" onkeydown="test(event,'txt1','form1')" /><br />
        <input id="txt2" name="text2" onkeydown="test(event,'txt2','form1')"/>
    </form>
      

  7.   

    既然用了  jquery  为何不完美运用呢<script src="jquery-1.3.2.js"></script>
    <script>
    function test(i){   var id =  $('#form1').find('input').eq(i).attr('id');
       var text = $('#form1').find('input').eq(i).attr('name');
       alert('你点击了的ID名称为'+id);
       alert("你点击的这个ID为"+id+"的name为"+text);   
    }
    </script><form id="form1" name="form1">
        <input id="txt1" name="text1" onkeydown="test(0);" /><br />
        <input id="txt2" name="text2" onkeydown="test(1)"/>
    </form>