我正在計算基於持續時間的工作日和週末折扣價格。所以,我給出了使用datepicker插件的持續時間和日期輸入在頁面上方。爲此我得到了正確的結果。但是我必須創建兩個不同的jsp頁面(date.jsp和cal.jsp)。 在第1個jsp頁面(date.jsp)中,我使用datepicker選擇日期。並在第二個jsp頁面(cal.jsp)如何將日期標識從一個jsp傳遞到另一個jsp頁面
我寫了一個method ->[caluculate(#dateid,#duratiionid)]
來計算輸入的時間作爲持續時間的價格。
這裏我的問題是如何將我通過[#dateid]
從1 JSP頁面(date.jsp)到第二jsp頁面(cal.jsp) ,這樣我可以在這個method->[caluculate(#dateid,#duratiionid)]
同時通過ID的。
<div id="container">
<div id="form">
<form id="book_court">
<div class="fieldset">
<fieldset>
<legend class="visuallyhidden">Booking Details</legend>
<h2>Booking Details</h2>
<p>
<label for="date">Date<br/><span id="dateNote">Firefox does not have a HTML5 datepicker <a href="https://support.mozilla.org/en-US/questions/986096">yet</a>.</span></label>
<input type="date" name="date" id="date" min="today" required />
</p>
<p>
<label for="tickets_duration"> Hours</label>
<input type="number" min="1" name="tickets_duration" id="tickets_duration" required />
</p>
<p>
<label>Total Price</label>
<span id="total_price">(enter data first)</span>
</p>
<div id="submit_wrapper">
<input type="submit" id="submit" value="Book Court" />
</div>
</fieldset>
</div>
</form>
</div>
</div>
<script id="worker" type="javascript/worker">
self.onmessage = function msgWorkerHandler(event){
var jsonString = event.data;
var day = jsonString.day;
var tickets_duration = jsonString.tickets_duration;
// set price of each hours as Rs. 200 and 300
var totalPriceOnWeekday = tickets_duration * 200;
var totalPriceOnWeekends=tickets_duration * 300;
// 10% discount if on weekday and 15% on weekends
if(day > 0 && day < 6){
totalPriceOnWeekday = totalPriceOnWeekday - 0.10 * totalPriceOnWeekday;
postMessage("₹ " + totalPriceOnWeekday);
}else if(day == 0 || day == 7){
totalPriceOnWeekends = totalPriceOnWeekends - 0.15 * totalPriceOnWeekday;
postMessage("₹ " + totalPriceOnWeekends);
}
}
</script>
<script>
$(document).ready(function(){
// first check the movies already book
// apply jQuery UI Redmond theme to 'Book Tickets' button
$("#submit").button();
// calculateTotalPrice on keyup or on change of movie/date/tickets
$("#date, #tickets_duration").change(calculateTotalPrice);
// on form submit
$("#book_court").submit(function(event){
// prevent on submit page refresh
event.preventDefault();
// check locally stored data
// clear the form
$('#book_court').each(function(){
this.reset();
});
// reset (enter data first) message
$("#total_price").html("(enter data first)");
// update movies booked list
});
// set minimum date in datepicker as today
var today = new Date().toISOString().split('T')[0];
document.getElementsByName("date")[0].setAttribute('min', today);
});
function calculateTotalPrice(){
if($("#tickets_duration").val() != "" && $("#date").val() != ""){
if(window.Worker){
// create web worker
var blob = new Blob(
[document.querySelector("#worker").textContent],
{type: 'text/javascript'});
var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function(event){
$("#total_price").html(event.data);
}
worker.onerror = function(errorObject){
$("#total_price").html("Error: " + errorObject.message);
}
var date = new Date($('#date').val());
// get day
var day = date.getDay();
// get number of booked shows
// send JSON data to worker
var jsonData = {'day': day, 'tickets_duration': Number($("#tickets_duration").val())};
worker.postMessage(jsonData);
}
}
}
</script>
感謝#你能否提供更多的細節?通過給出示例代碼。 – Kumar
此流程中是否包含任何服務器端代碼?如果是這樣,您可以將選定日期發佈到服務器,然後在那裏進行計算並顯示在cal.jsp中。如果沒有服務器端代碼,則可以使用「document.cookie」將選定的日期保存在date.jsp中,然後使用相同的「document.cookie」在cal.jsp中檢索它以進行計算。 – DoT
是的。我使用servlet和MySQL來存儲日期。但我不知道如何計算服務器端的價格(工作日+週末)??? ...你能給我一個代碼示例..謝謝 – Kumar