点击按钮后增加计数器值 [英] Increase counter value upon button click

查看:116
本文介绍了点击按钮后增加计数器值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法获得我需要工作的jQuery位。我之前从未使用过jQuery,但是我希望在我的页面上有一个按钮,当单击时,将最初的0值增加1。我的问题类似于 jQuery - 增加单击按钮时计数器的值按文本框中的按钮和值增加但我无法理解我做错了什么。这就是我所拥有的:

I cannot seem to get the jQuery bit I need to work. I've never used jQuery before, but I'd like to have a button on my page that, when clicked, increments an initially 0 value by one. My question is similar to jQuery - Increase the value of a counter when a button is clicked and press button and value increases in text box but I can't make sense of what I'm doing wrong. Here's what I have:

<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>

非常感谢任何帮助。请原谅任何巨大的明显错误,我不知道如何做到这一点。谢谢,快乐的消息!=)

Any help is greatly appreciated. Please forgive any huge glaring mistakes, I have no idea how to do any of this yet. Thank you, and happy tidings! =)

推荐答案

很明显你是jQuery的新手,所以好的阅读可能对你有所帮助,我建议先行jQuery的

It's clear you're new to jQuery, so a good read might be of help to you, I would recommand Head First jQuery.

关于这个问题,我重写了你的代码,这里应该是这样的:

As for the question, I rewrote your code, here's what it should be like :

<!DOCTYPE html>
<html>
    <head>
        <title> My first increment </title>
    </head>
    <body>

        <input type="text" name="TextBox" id="TextBox" value="0" />
        <input type="Button" id='AddButton' value="+" />

        <script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
        <script>
            $(document).ready(function(){
                $('#AddButton').click( function() {
                    var counter = $('#TextBox').val();
                    counter++ ;
                    $('#TextBox').val(counter);
                });
            });
        </script>
    </body>
</html>

这是一个小测试:测试我

想法是增加一个变量而不是一个对象,你也有一些语法错误,但尝试此代码,我们可以在评论中进一步讨论。

The idea is to increment a variable not an object, and you have some syntax mistakes as well, but try this code and we can discuss it further on comments.

祝你好运。

这篇关于点击按钮后增加计数器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆