Java脚本添加日期到目前为止不会产生结果

时间:2015-06-23 09:25:50

标签: javascript html

请有人帮忙 我写了一些JavaScript,它应该将写入表单的天数添加到预订的开始日期。然后它应该输出返回日期。我没有看到任何语法错误,但脚本没有运行。请有人帮我确定问题的根源。

的JavaScript

<script type="application/javascript">
function  ReturnDate(){
    var reservationStart = document.getElementById('ReservationStart').value;
       var requestedDays = parseInt(document.getElementById('days').value);
       var returnDate = document.getElementById('ReturnDate');
       var arrParts = reservationStart.split("/");
       var myDate = new Date(arrParts[2], parseInt(arrParts[1])-1, parseInt(arrParts[0]) + requestedDays, 0, 0, 0, 0);
       var sResult = ("0"+myDate.getDate()).substr(-2)+"/";
       sResult += ("0"+(myDate.getMonth()+1)).substr(-2)+"/";
       sResult += myDate.getFullYear();
       document.getElementById('ReturnDate').value =   sResult;
    }
</script>

HTML

<form name="frmHTML" method="post" action="">
        <table id="tables" class="form" style="width:100%">
        <tr> 

<td>Game ID</td> 
<td><input type="text"
required autocomplete="off" 
value= "<?php echo (isset($ID))?$ID:'';?>"
name="gameID"
id="gameID"
placeholder= "Enter A Number between 1 and 8"
style="width:80%"/></td>
</tr>

<tr>    
<td></td>
<td><input type="submit"
value= "Search For Game"
name= "FindDetails"
id= "FindDetails"
style= "width:80%" /></td>
</tr>

<tr>
<td>Game Name</td>
<td><input type= "text"
value = "<?php echo (isset($GameName))?$GameName:'';?>"
name="GameName"
id= "GameName"
readonly
style "width:80%" /></td>
</tr>

<tr>
<td>Game Rental Cost(per day)</td>
<td><input type= "text"
value = "<?php echo (isset($GameCost))?$GameCost:'';?>"
name="gameCost"
id="gameCost"
readonly
style= "width:80%" /></td>
</tr>

<tr>
<td>Number of days</td>
<td><input type= "text"
name="days"
id="days"
placeholder="Enter the number of days you wish to borrow the game for"
onkeyup = "mycalculate()"
autocomplete="off"
style="width:80%" /></td>
</tr>

<tr>
<td>Total Cost</td>
<td><input type="text"
value=""
name="total"
id= "total"
readonly
style="width:80%"/></td>
</tr>

<tr>
<td>Your Name</td>
<td><input type="text"
value=""
name="StudentName"
id="StudentName"
autocomplete="off"
style="width:80%"/></td>
</tr>

<tr>
<td>Date Start(dd/mm/yyyy)</td>
<td><input type="text"
value=""
name="ReservationStart"
id="ReservationStart"
onkeyup = "ReturnDate()"
autocomplete="off"
style="width:80%"/></td>
</tr>         


<tr>
<td>Date End(dd/mm/yyyy)</td>
<td><input type="text"
value=""
name="ReturnDate"
id="ReturnDate"
style="width:80%"/></td>
</tr>

<tr>    
<td></td>
<td><input type="submit"
value= "Book Game Now"
name= "Submit"
id= "Submit"
style= "width:80%" /></td>
</tr>
</table>
</form>

1 个答案:

答案 0 :(得分:1)

问题是函数名称与其中一个变量相同。这意味着计算机感到困惑。

解决方案:

<强>的JavaScript

<%= link_to 'delete', cvsdestroy_path(p), method: :delete %>

<强> HTML

<script>
    function myReturnDate(){
       var reservationStart = document.getElementById('ReservationStart').value;
       var requestedDays = parseInt(document.getElementById('days').value);
       var returnDate = document.getElementById('ReturnDate');
       var arrParts = reservationStart.split("/");
       var myDate = new Date(arrParts[2], parseInt(arrParts[1])-1, parseInt(arrParts[0]) + requestedDays, 0, 0, 0, 0);
        var sResult = ("0"+myDate.getDate()).substr(-2)+"/";
          sResult += ("0"+(myDate.getMonth()+1)).substr(-2)+"/";
       sResult += myDate.getFullYear();
        document.getElementById('ReturnDate').value =   sResult;
       }


    </script>