<html>
<head>
<title>Calculator</title>
<style type = "text/css">
#calculator{
width:164px;
top:20%;
right:50%;
position:absolute;
border:1px black solid;
background-image:url('images.jpg');
}
#matrix
{
float:right;
border:1px black solid;
width:100px;
}
#row1{
position:relative;
width:17px;
}
</style>
</head>
<body>
</div>
<div id = "matrix">
<form>
<table>
<tr><td style = "width:40px"><input type = "text" id = "one"></input></td>
<input type = "number" id = "two"></input></td></tr>
<tr><td><input type = "number" id = "three"></input></td>
<input type = "number" id = "four"></input></td></tr>
</table>
<input type = "submit" value = "count" onclick = "calculate();"></input>
</form>
</div>
<script language = "javascript" type = "text/javascript">
function calculate(){
var a = document.getElementById('one').value;
alert(a);
}
</script>
</body>
</html>
在我拥有的函数中,我不能通过id正确地访问any元素,所以当我试图警告变量时,一个警告框是空的。我之前把我的javascript代码放在头上,但它不起作用,neither.Moving it to a body section没有改变situation.Would。如果有人能帮助我修复javascript中的这个错误,请不要把jquery代码写成alternative.Thanx。
发布于 2013-04-19 22:32:20
为输入添加id
并将其更改为button
<input id="count" type="button" value="count">
并使用以下命令:
let count = document.getElementById('count');
let a = document.getElementById('one');
count.addEventListener('click', calculate, false);
function calculate() {
alert(a.value);
}
在jsFiddle上查看
发布于 2013-04-19 22:32:15
您的按钮是提交按钮。您应该将其更改为一个按钮:
<input type = "button" value = "count" onclick = "calculate();" />
除此之外,你应该修改你的HTML,因为它的格式非常糟糕,也许你有一些东西可能会在以后引起错误。
https://stackoverflow.com/questions/16114361
复制