2015-06-23 27 views
4

請有人可以幫助 我已經寫了一些JavaScript,它應該將寫入我的表格的天數添加到保留的開始日期。它應該輸出回報日期。我沒有看到任何語法錯誤,但腳本無法運行。請有人幫助我找出問題的根源。添加日期的Java腳本不會產生結果

的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

問題是函數名是一樣的變量之一。這意味着電腦會感到困惑。

解決辦法:

的JavaScript

<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> 

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 = "myReturnDate()" 
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> 
相關問題