我创建了一个基本的JavaScript计数器,它计算按下按钮的次数,然后将其显示在输入框中。
我想更改它以便计算按下的次数并将其显示为文本。
例如5,123
HTML:
<div class="col-md-2 col-md-offset-3">
<div class="outer"><img src="assets/img/f1.jpg" alt="..." class="img-rounded" style="width:250px;height:150px"></div>
<div class="inner">
<center><button class="btn btn-default" value = "Click" onclick = "count()">Vote</button></center>
<input id = "counting" type = "text" class="votecount" />
</div>
</div>
CSS:
.outer {
position:absolute;
}
.inner {
position:relative;
margin-top:50px;
}
.votecount {
width:50px;
}
JAVASCRIPT:
<script type ="text/javascript">
var x = 0;
function count()
{
x += 1;
document.getElementById( "counting" ).value = x;
}
</script>
答案 0 :(得分:1)
使用类似的东西..首先获得计数值..如果没有投票..在其中添加1 ..如果已经投票..增加到它
function count()
{
var x = document.getElementById("counting").value;
if(x==""){
document.getElementById( "counting" ).value = 1;
}else{
document.getElementById( "counting" ).value = parseInt(x)+1;
}
}
的链接
答案 1 :(得分:0)
变化:
<input id = "counting" type = "text" class="votecount" />
要:
<span id = "counting" class="votecount"></span>
答案 2 :(得分:0)
将输入设为span并更改您的DOM查询以更改&#34; innerHTML&#34;:
function count()
{
x += 1;
document.getElementById( "counting" ).innerHTML = x;
}