function Total()
{
var num=document.getElementById("num").value
var pr=document.getElementById("price").value
var total=document.getElementById("total")
var tt=(num * pr).toFixed(2)
total.value=tt
}
</script>
</head>
<body>
数量<input type="text" id="num" name="num" value="0" />
单价<input type="text" id="price" name="price" onchange="Total()" />
总价<input type="text" id="total" name="total" />
</body>
这是只要单价改变自动得到总价,取两位小数
第一种:这样的代码用多了比较耗时,尽量少用;<head>
<script>
window.onload=function(){
var oInput=document.createElement("input")
oInput.type="text"
document.body.appendChild(oInput)
}
</script>
</head>
第二种:这种也行
<head>
<script>
window.onload=function(){
document.body.innerHTML="<input type='text' />"
}
</script>
</head>
<!Doctype HTML><html>
<head>
<title>文本框宽度自动适应文本宽度 </title>
</head>
<script type="text/javascript">
function changeInputlength(cursor)
{
var getcount=document.getElementById("countFont")
var getText=document.getElementById("text")
getcount.innerHTML='<font>第'+(parseInt(getText.value.length)+1)+'个字符</font>'
cursor.size=getText.value.length+2
}
</script>
<body>
请输入字符:<input type="text" size="3" id="text" onkeydown="changeInputlength(this)"/>
<span id="countFont"></span>
</body>
</html>