我有以下代码: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>
我想做的是:
我哪里错了?
答案 0 :(得分:9)
您只是递增一个在函数结束时消失的局部变量。你可以这样做:
var a = 1;
function increase(){
var textBox = document.getElementById("text");
textBox.value = a;
a++;
}
答案 1 :(得分:6)
<input type="text" id="text" value="1"/>
function increase(){
var textBox = document.getElementById("text");
textBox.value++;
}
答案 2 :(得分:0)
最好检查文本字段的值。如果它为空a=1
其他textfield.value++
答案 3 :(得分:0)
我的意思是var a=1;
应该在函数
答案 4 :(得分:0)
你可以使用它。只需复制并粘贴
<html>
<head>
<script type="text/javascript">
var a = 1;
function increase(){
document.getElementById("text").value=a;
a=a+1;
}
</script>
</head>
<body>
<button type="button" onclick="increase()">show</button>
<input type="text" id="text">
</body>
</html>
答案 5 :(得分:0)
使用此代码
var k = 1;
function shashi() {
document.getElementById('para').innerHTML = k;
k++;
}
k = k;
</script><br/>
并在点击事件
上调用shashi()函数答案 6 :(得分:0)
您应该练习在必要时不要声明全局变量。它有时会导致大型应用程序出现漏洞。 JS函数也是对象,这意味着它们可以具有属性。
let MainFunInterest = "MainFunInterest"
let SomethingInterestingIhaveRead = "SomethingInterestingIhaveRead"
let JobOrEducation = "JobOrEducation"
let WhatIamConsideringBuying = "WhatIamConsideringBuying"
let post : [String : AnyObject] = ["MainFunInterest" : MainFunInterest as AnyObject,
"SomethingInterestingIhaveRead" : SomethingInterestingIhaveRead as AnyObject,
"JobOrEducation" : JobOrEducation as AnyObject,
"WhatIamConsideringBuying" : WhatIamConsideringBuying as AnyObject]
let databaseRef = Database.database().reference()
databaseRef.child("personal info").childByAutoId().setValue(post)
}
答案 7 :(得分:0)
p {
font-weight: 400 /* 407 is not a font-weight value */;
font-style: normal;
font-size: 3vw; /* use vw-relative units to match other text elements on the page */
color: rgb(40, 40, 40);
}
答案 8 :(得分:0)
你在按钮代码中的increase()函数前后也加了引号
答案 9 :(得分:-1)
您可以使用更简单的方法来实现递增和递减按钮。 使用内联javascript的简单示例:
myDb = new DatabaseHelper(this);