我有以下代码:
JS Fiddle
<html>
<head>
<script>
function increase(){
var a = 1;
var textBox = document.getElementById("text");
textBox.value = a;
a++;
}
</script>
</head>
<body>
<button type="button" onclick="increase()">show</button>
<input type="text" id="text">
</body>
</html>
我想要做的是:
>单击按钮时,“a”的值将显示在文本框中,“a”将递增.
>现在再次单击时,应显示递增的值,但不会发生这种情况.
我哪里错了?
解决方法
你只是递增一个在函数结束时消失的局部变量.你可以这样做:
var a = 1;
function increase(){
var textBox = document.getElementById("text");
textBox.value = a;
a++;
}