在按钮上单击增加日+1 [英] On button click increase day with +1

查看:75
本文介绍了在按钮上单击增加日+1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何点击按钮点击日期增加+1



右边没有按下我的按钮,我有这个代码



How could i increase button click date by +1 on click

Right not in my button click, i have this code

 function addNewDateFrom(){

    var numberToAddafter = 0;
    var toTime = new date();
    toTime.setDate(toTime.getDate() + numberToAddafter++);
}

var textNodeAddFreeTime = document.createTextNode("+");
var buttonCreateAddFreeTime = document.createElement("button");

buttonCreateAddFreeTime.appendChild(textNodeAddFreeTime);
 buttonCreateAddFreeTime.onclick = addNewDateFrom;







我第一次点击按钮获得1

但是第二次还是1?



为什么?




The first time i click the button gets 1
But second time still 1?

Why?

推荐答案

试试

try
var toTime = new Date();

function addNewDateFrom(){
    toTime.setDate(toTime.getDate()+1);
}





DEMO [ ^ ]


这真的是Java还是javascript?它看起来像后者。

Is this really Java or javascript? It looks like the latter to me.
function addNewDateFrom(){
    var numberToAddafter = 0;  // set number to add as 0
    var toTime = new date();   // get a new time
    toTime.setDate(toTime.getDate() + numberToAddafter++);  // add zero to the time
}



每次使用 numberToAddafter 中的新值(零)开始,因为这是一个局部变量,在将其添加到局部变量后将其递增为1时间,没有用处,因为变量会立即删除。


You start each time with a new (zero) value in numberToAddafter, and since this is a local variable, incrementing it to 1 after you add it to the time, serves no purpose, as the variable is immediately deleted.




试试这个

Hi,
try this
var toTime = new Date();
Var counter=0
function addNewDateFrom(){
    toTime.setDate(toTime.getDate()+counter);
counter=counter+1;
 }

-


这篇关于在按钮上单击增加日+1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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