在html文件中如何通过表单把一个数字赋值给一个javascript中的变量!

script在head中; input 在body中 带着提交按钮

第1个回答  推荐于2016-01-28
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>未命名页面</title>
<script language="javascript" type="text/javascript">
var t;

function setValue()
{
t = document.getElementById("tt").value;
alert(t);
}
</script>
</head>
<body>
<form id="form1">
<div>
<input type="text" id="tt" value="10" />
<input type="button" id="btnSub" value="提交" onclick="setValue()" />
</div>
</form>
</body>
</html>

点击“提交”按钮,文本框内的值将会赋给<head></head>标签内脚本变量 t 。你可以将此代码复制到文本文件里然后另存为.html,打开.html文件进行测试。本回答被提问者采纳
第2个回答  2011-05-19
用javascript的document对象来获取你的数值,
第3个回答  2011-05-19
<input type=text id=t value=2 />
<script>
var n=document.getElementById('t').value;
alert(n);
</script>追问

script在head中; input 在body中 呢?就是顺序颠倒一下 带着提交按钮 怎么弄 谢谢

相似回答