简体   繁体   中英

After each times click an increasing number?

I want after each times click value 2011 an increasing number, how is it?

Example:

$('button').click(function() {
    var num = '2011';
    var out = num ++1;
    alert(num); // i want this output: 2012
});
var num = 2011;
$('button').click(function() {
    num++;
    alert(num); // i want this output: 2012
});
var num = '2011';
$('button').click(function() {
    num++;
    alert(num); // i want this output: 2012
});

Try this:

var num = '2011';
$('button').click(function() {
    var out = num++;
    alert(num);
});
  • var num must be outside of the function, otherwise it will be 2011 again every time the function is run.
  • num++1 should be num++ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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