87,910
社区成员
发帖
与我相关
我的任务
分享
<!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>
<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>