[英]How to Change a specific value in input using JavaScript
I have an input containing a date value我有一个包含日期值的输入
example:例子:
<input type="text" name="cel_dafa" value="26/12/2018">
I want a button when pressed it increases the value one month我想要一个按钮,按下它会增加一个月的价值
So that the result after the pressure:使压后的结果:
<input type="text" name="cel_dafa" value="26/1/2019">
Note: I do not want to harm the day, Only the month and year if at the end of the year注:我不想伤天,只有月和年如果在年末
Ok, so consider you have a button like;好的,所以考虑你有一个按钮;
<button onclick="increase()"> Click Me </button>
And input like;和输入一样;
<input type="text" id="date" name="cel_dafa" value="26/12/2018">
Now code of increase function;现在增加函数的代码;
function increase()
{
var currentDate = document.getElementById('date').value;
var parts = currentDate.split("/");
var day = parts[0];
var month = parts[1];
var year = parts[2];
var d = new Date(year, month - 1, day);
d.setMonth(d.getMonth() + 1);
var newDate = d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear();
document.getElementById('date').value = newDate;
}
Html html
<input type="text" id="date-input" name="cel_dafa" value="26/12/2018">
<button onclick="increaseDate()">submit</button>
JS JS
function increaseDate() {
var dateInput = document.querySelector('#date-input').value;
var day = dateInput.split('/')[0];
var month = dateInput.split('/')[1];
var year = dateInput.split('/')[2];
if (month > 11) {
year = parseInt(year) + 1;
month = 1
} else {
month = parseInt(month) + 1;
}
var newDate = day +"/"+ month +"/"+ year;
document.querySelector('#date-input').value = newDate;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.