简体   繁体   English

点击增加数量

[英]click to increase number

How to increasing 100 each time when I clicked ? 如何在每次单击时增加100? Any mistake I have make ? 我有任何错误吗? Please tell me thanks 请告诉我谢谢

 $(".label").click(function () { var number = 100; number++; $(".shownumber").text(number); }); 
 .label{ width:auto; } .label:hover{ background:#ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="label">click me</label> <div class="shownumber">100</div> 

The question is ambiguous as to whether you want to add 1 or 100, but in any case, you can always add directly to the text value (converted to an integer). 有关是否要添加1或100的问题是模棱两可的,但是在任何情况下,您始终可以直接将其添加到文本值(转换为整数)。 I am typically against using global vars for anything, as they do not scale (ie to multiple elements). 我通常反对将全局变量用于任何对象,因为它们无法扩展(即扩展到多个元素)。

text allows you to supply a function, which will return the new text for the element: text允许您提供一个函数,它将返回元素的新文本​​:

$(".label").click(function () {
    $(".shownumber").text(function(){return ~~$(this).text() + 1}); // or 100!
});

Note: ~~ is a handy (and fast) shortcut conversion from string to integer. 注意: ~~是从字符串到整数的便捷(且快速)的快捷方式转换。

JSFiddle: https://jsfiddle.net/yuqr1p58/4/ JSFiddle: https ://jsfiddle.net/yuqr1p58/4/

With this approach you can support multiple elements, using some form of ownership arrangement. 通过这种方法,您可以使用某种形式的所有权安排来支持多个要素。 This one uses a containing div and closest and supports any number of these controls. 此控件使用一个包含divclosest的容器,并支持任意数量的这些控件。 This simply would not work using a global var. 使用全局变量根本无法工作。

JSFiddle: https://jsfiddle.net/yuqr1p58/5/ JSFiddle: https ://jsfiddle.net/yuqr1p58/5/

You were in the right path, but you have to move var number = 100; 您的路径正确,但必须移动 var number = 100; outside the scope of click function like below. 超出了以下click功能的范围 Also you have to use number += 100; 另外,您必须使用number += 100; , to increment by 100 , instead of number++ which just increment by 1. ,以增加100 ,而不是number++ ,后者仅增加1。

var number = 100;
$(".label").click(function () {
   number += 100;
   $(".shownumber").text(number);
});

 var number = 100; $(".label").click(function () { number += 100; $(".shownumber").text(number); }); 
 .label{ width:auto; } .label:hover{ background:#ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="label">click me</label> <div class="shownumber">100</div> 


If you want to keep number++ you can multiply number by 100 before insterting in page ( .text(number*100) ). 如果你想保持number++ ,你可以乘number100页insterting之前( .text(number*100)

 var number = 1; $(".label").click(function () { number++; $(".shownumber").text(number*100); }); 
 .label{ width:auto; } .label:hover{ background:#ccc; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="label">click me</label> <div class="shownumber">100</div> 

You just ned to move the var num outside the scope of the click . 您刚刚将var num移到click范围之外。

$(document).ready(function(){
  var number = 100;
$(".label").click(function() {

        number++;
        $(".shownumber").text(number);

    });
});

Try this. 尝试这个。

var number = 100;
    $(".label").click(function () {

                number += 100;
                $(".shownumber").text(number);

            });

https://jsfiddle.net/yuqr1p58/6/ https://jsfiddle.net/yuqr1p58/6/

$(".label").click(function () {
    var number = 100;
    var oriNum= parseInt($(".shownumber").text())+number;
    $(".shownumber").text(oriNum);

});

Demo 1 演示1

with single line of Code 单行代码

$(".label").click(function () {
    $(".shownumber").text( parseInt($(".shownumber").text())+100);
});

Demo 2 演示2

   var number = 100;

$(".label").click(function () { $(“。label”)。click(function(){

    number += 100;
    $(".shownumber").text(number);

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM