I have a trouble with a custom function that I've done.
In fact it does not work and I really do not know why.
Here is the function:
<script type="text/javascript">
function lz(x){
return x.toString().replace(/^(\d)$/,'0$1')
}
function dayplus(){
var items = document.getElementsByClassName("datepicker hasDatepicker");
for (var i = 0; i < items.length; i++){
if (items[i].getAttribute('required')){
var itemDtParts = items[i].value.split("-");
var itemDt = new Date(parseInt(itemDtParts[2],10), parseInt(itemDtParts[1],10)-1, parseInt(itemDtParts[0],10)+ +nb);
items[i].value = lz(itemDt.getDate())+"-"+lz(itemDt.getMonth()+1)+"-"+itemDt.getFullYear();
}
}
}
</script>
It says to me:
Uncaught SyntaxError: Unexpected token ILLEGAL addday.html:20
Moreover it says to me that the function is undefined or it is not the case.
Here below is the form I use with the function:
<input type="text" class="datepicker hasDatepicker" required value="26-10-2012">
<input type="button" value="( - )" width="22" height="22" onClick="subday()" />
<input name="jours" type="text" value="" size="5" id="nb" />
<input type="button" value="( + )" width="22" height="22" onClick="dayplus()" />
I made a fiddle here
The first thing I noticed, is that your markup looks strange. Shouldn't it be
<input type="text" class="datepicker hasDatepicker" required="true" value="26-10-2012">
instead of
<input type="text" class="datepicker hasDatepicker" required value="26-10-2012">
The second thing is the invalid syntax, as pointed out by enhzflep and Kimo_do, nb
is not defined and + + nb
. I replaced it by 1
in the fiddle.
,10)+ +nb);
should be ,10)+ nb);
perhaps? or ,10)+ (+nb) );
if nb may be negative. That is to say - it looks like an added +. On closer looking, I'm also wondering if you actually mean nb.value or it's longhand: document.getElementById('nb').value?
Or in code:
var itemDt = new Date(parseInt(itemDtParts[2],10), parseInt(itemDtParts[1],10)-1, parseInt(itemDtParts[0],10) + document.getElementById('nb').value) );
Modified code: jsfiddle . changes: nb
was missing, you were using + +
to add nb
<input type="text" class="datepicker hasDatepicker" required=true value="26-10-2012">
<input type="button" value="( - )" width="22" height="22" onClick="subday()" />
<input name="jours" type="text" value="" size="5" id="nb" />
<input type="button" value="( + )" width="22" height="22" onClick="dayplus()" />
<script type="text/javascript">
function lz(x){
return x.toString().replace(/^(\d)$/,'0$1');
}
function dayplus(){
var items = document.getElementsByClassName("datepicker hasDatepicker");
nb = 1;
for (var i = 0; i < items.length; i++){
if (items[i].getAttribute('required')){
var itemDtParts = items[i].value.split("-");
var itemDt = new Date(itemDtParts[2], parseInt(itemDtParts[1] ,10)-1, parseInt(itemDtParts[0],10) +nb);
items[i].value = lz(itemDt.getDate())+"-"+lz(itemDt.getMonth()+1)+"-"+itemDt.getFullYear();
}
}
}
</script>
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.