<!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,form){
   var temp_form = form;
   var temp1 = document.form1.txt1.value;//txt1和form1都不是变量
   alert("非变量的值为:"+temp1)
   var temp2 = document.form1[id].value;//txt1为变量 
   alert("txt1为变量 =="+temp2)
   var temp3 = document[temp_form].id.value;//form1为变量
   alert("form1为变量=="+temp3);
   var temp4 = document[temp_form.id].value;//form1和txt1都为变量
   alert("form1和txt1都为变量=="+temp4)
}
</script>
</head>
<body>
<form id="form" name="form1">
    <input id="txt1" name="text1" onkeydown="test(event,$(this).attr('id'),$(this).parent().attr('name'));" /><br />
    <input id="txt2" name="text2" onkeydown="test(event,$(this).attr('id'),$(this).parent().attr('name'));"/>
</form>
</body>
</html>我想得到
   var temp3 = document[temp_form].id.value;//form1为变量
   alert("form1为变量=="+temp3);
   var temp4 = document[temp_form.id].value;//form1和txt1都为变量
   alert("form1和txt1都为变量=="+temp4)
的值请大家看看,谢谢

解决方案 »

  1.   

    直接利用 $('#id').val()即可呀。
    var temp3 = $('#'+id).val();//form1为变量
    alert("form1为变量=="+temp3);
      

  2.   

    var temp3 = document[temp_form].id.value;//form1为变量
    改为: var temp3 = document[temp_form].getElementById(id).value; //这里id是字符串
      

  3.   

    我就是想取到temp4的值,但是form1和txt1是有动态取到的,如果是静态的就可以
    var temp1 = document.form1.txt1.value  取到temp1的值
    现在把form1 和txt1 都改成变量怎么才能取到
      

  4.   

     var temp3 = document[temp_form].text1.value;//form1为变量
       alert("form1为变量=="+temp3);
       var temp4 = document[temp_form][id].value;//form1和txt1都为变量
       alert("form1和txt1都为变量=="+temp4)
      

  5.   

    var temp1 =document.all[temp_form][id].value
      

  6.   

    我这里没有JQ,
    你可以试一试下面代码:
    应该可以的。
    [code=<!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,txt,formt){
       var temp4 = document.forms[formt].elements[txt].value;//form1和txt1都为变量
       alert("form1和txt1都为变量=="+temp4)
    }
    </script>
    </head>
    <body>
    <form id="form" name="form1">
        <input id="txt1" name="text1" onkeydown="test(event,$(this).attr('id'),$(this).parent().attr('name'));" /><br />
        <input id="txt2" name="text2" onkeydown="test(event,$(this).attr('id'),$(this).parent().attr('name'));"/>
    </form>
    </body>
    </html>
    HTML][/code]
      

  7.   

    这里是关键:function test(e,txt,formt){
      var temp4 = document.forms[formt].elements[txt].value;//form1和txt1都为变量
      alert("form1和txt1都为变量=="+temp4)
    }
      

  8.   

    ff不行try:
    var temp4 = eval("(document."+temp_form+"['"+id+"'].value)")
      

  9.   

    var temp4 =document.[temp_form][id].value
    谢谢大家,这样就可以了
      

  10.   

    不知道你的问题有什么意义,
    用Jquery还写普通js代码,
    用Jquery语法写多简单