admin管理员组文章数量:1401484
So I'm trying to create a counter using jQuery but I can't get it to work for some reason. I'm using 3 inputs. 1 number input and 2 buttons.
My html looks like this:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="title">
<div id="middle">
<div id="inner">Books</div>
</div>
</div>
<input class="counter" type="number" value="0"></input>
<input class="red" type="submit" value="-"></input>
<input class="green" type="submit" value="+"></input>
<script type="text/javascript" src="jquery-1.11.10.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
And this is my jQuery code:
$(document).ready(function(){
var num = 0;
$(".red").click(function(){
num = $(".counter").val();
num = num - 1;
$(".counter").val(num);
})
$(".green").click(function(){
num = $(".counter").val();
num = num + 1;
$(".counter").val(num);
})
}
I can't seem to find what's wrong with my code.
So I'm trying to create a counter using jQuery but I can't get it to work for some reason. I'm using 3 inputs. 1 number input and 2 buttons.
My html looks like this:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="title">
<div id="middle">
<div id="inner">Books</div>
</div>
</div>
<input class="counter" type="number" value="0"></input>
<input class="red" type="submit" value="-"></input>
<input class="green" type="submit" value="+"></input>
<script type="text/javascript" src="jquery-1.11.10.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
And this is my jQuery code:
$(document).ready(function(){
var num = 0;
$(".red").click(function(){
num = $(".counter").val();
num = num - 1;
$(".counter").val(num);
})
$(".green").click(function(){
num = $(".counter").val();
num = num + 1;
$(".counter").val(num);
})
}
I can't seem to find what's wrong with my code.
Share Improve this question asked Feb 5, 2014 at 19:18 RorrimRorrim 151 gold badge2 silver badges6 bronze badges 1-
remove these:
num = $(".counter").val();
– Malk Commented Feb 5, 2014 at 19:22
3 Answers
Reset to default 4Here I have use +
, which Force the engine to implicitly convert it by applying a math operator to it;
Convert input value to number
num = +$(".counter").val();
DEMO
Additionally, You are missing closing )
of document ready handler.
I would also like to suggest you should use type="button"
instead of type="submit"
The reason it's failing is because the val()
is returned as a string and the +
is doing string concatenation, not addition.
However, since you already have the value stored in a variable, you really don't need to pull the value from the input each time. Here's a simpler version:
http://jsfiddle/bcHWa/
var num = 0;
$(".red").click(function () {
$(".counter").val(--num);
})
$(".green").click(function () {
$(".counter").val(++num);
})
num = $(".counter").val();
is going to be a string, therefore "1" + "1" = "11"
Try:
num = parseInt($(".counter").val());
本文标签: javascriptHow to create a counter using jQueryStack Overflow
版权声明:本文标题:javascript - How to create a counter using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744207844a2595275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论