【发布时间】:2013-07-14 14:06:06
【问题描述】:
我似乎无法获得工作所需的 jQuery 位。我以前从未使用过 jQuery,但我希望在我的页面上有一个按钮,当单击该按钮时,最初的 0 值会增加 1。我的问题类似于jQuery - Increase the value of a counter when a button is clicked 和press button and value increases in text box,但我无法理解我做错了什么。这是我所拥有的:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$("#update").click(function()
{
$("#counter")++;
}
</script>
</head>
<body>
<input type="text" name="TextBox" id="TextBox" value="0" />
<input type="Button" id='AddButton' value="+" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$('#AddButton').on('click', function ()
{
var input = $('#TextBox');
input.val(parseInt(input.val()) + 1);
})
</script>
</body>
</html>
非常感谢任何帮助。请原谅任何明显的明显错误,我还不知道该怎么做。谢谢你,祝你好消息! =)
【问题讨论】:
-
元素
update和counter在哪里?以及为什么包含两次 jquery? -
$("#counter")返回 ID 为“counter”的(包装的)DOM 元素的句柄。该元素可能具有可以递增的数字属性,但该元素本身不是数字,因此您不能递增它。 -
为什么要投反对票?好吧,有错误,但老实说,问答格式得到尊重,有代码......我们可以帮助引导人们理解和改善自己,毕竟这不就是STO的意义吗?跨度>
标签: javascript jquery