<!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>
这个不是吗?楼上的请你看清楚再说
temp_id要用temp_name
form元素后只能跟name
为什么要这么复杂?不是temp1=temp_id就可以了吗?
不明白……
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>
<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>