简体   繁体   English

用于更改日期的javascript自定义函数

[英]javascript custom function for changing dates

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 . 第二件事是无效的语法,如enhzflep和Kimo_do所指出的那样,未定义nb+ + nb I replaced it by 1 in the fiddle. 我在小提琴中将其替换为1

,10)+ +nb); should be ,10)+ nb); 应该是,10)+ nb); perhaps? 也许? or ,10)+ (+nb) ); ,10)+ (+nb) ); if nb may be negative. 如果nb可能为负。 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? 仔细观察,我还想知道您实际上是指nb.value还是它的缩写: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 . 修改后的代码: jsfiddle changes: nb was missing, you were using + + to add nb 更改: nb丢失,您正在使用+ +添加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>

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

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