我正在使用javascript单击按钮更改日期。 我的问题是,页面加载时会显示今天的日期,而当我单击按钮时它将更改日期并通过url发送值。到目前为止,一切正常,但是重新加载页面后,它将再次显示今天的日期。代替它,它应该显示更改的日期。 最近两天我一直在为此工作,但没有任何适当的解决方案。 这是我的js代码:
document.getElementById("up").onclick = function() {
var i = dataI.valueOf() + 86400000 ;
dataI = new Date(i);
document.getElementById("dateD").innerHTML = dataI.toDateString();
var b = dataI.toDateString();
window.location="datechange.php?date=" + b;
}
document.getElementById("down").onclick = function() {
var i = dataI.valueOf() - 86400000 ;
dataI = new Date(i);
document.getElementById("dateD").innerHTML = dataI.toDateString();
var c = dataI.toDateString();
window.location.href ="datechange.php?previous=" + c;
}
var dataI = new Date();
document.getElementById("dateD").innerHTML = dataI.toDateString();
这是我的身体:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<input type=button id=down value=down >
<span id=dateD></span>
<input type=button id=up value=up >
<div id="div1-wrapper">
<div id="abcd">
<?php
$date2 = $_GET['date'];
echo $date2;
$date3 = $_GET['previous'];
echo $date3;
?>
</div>
</div>
答案 0 :(得分:0)
我认为您可以通过将日期存储在如下所示的会话中来做到这一点,我对您的代码进行了更改,看看它是否可以解决您的问题....
<?php
session_start();
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<input type="button" id="down" value="down" >
<?php
$date = '';
if(isset($_GET['date'])) { $date = $_GET['date']; };
if(isset($_GET['previous'])){ $date = $_GET['previous']; };
if(isset($date)){
$_SESSION['dt']=$date;
}
?>
<span id=dateD><?php echo $_SESSION['dt'];?></span>
<input type="button" id="up" value="up" >
<div id="div1-wrapper">
<div id="abcd">
<?php
echo $date;
?>
</div></div>
<script language="javascript">
document.getElementById("up").onclick = function(){
var i = new Date(document.getElementById("dateD").innerHTML).valueOf() + 86400000 ;
dataI = new Date( i);
document.getElementById("dateD").innerHTML = dataI.toDateString();
var b = dataI.toDateString();
window.location="file.php?date="+b;
}
document.getElementById("down").onclick = function(){
var i = new Date(document.getElementById("dateD").innerHTML).valueOf() - 86400000 ;
dataI = new Date( i);
document.getElementById("dateD").innerHTML =dataI.toDateString();
var c = dataI.toDateString();
window.location.href ="file.php?previous="+c;
}
var dataI = new Date();
if(document.getElementById("dateD").innerHTML == ''){
document.getElementById("dateD").innerHTML = dataI.toDateString();
}
if(document.getElementById("dateD").innerHTML == dataI.toDateString()){
document.getElementById("down").style.display='none';
}
</script>
答案 1 :(得分:0)
这是jquery中的脚本,可以解决您的问题。
var dataI = new Date();
var current_tm = dataI.valueOf();
$(function(){
var urlParams = new URLSearchParams(window.location.search);
var display_date;
if(urlParams.has('date')){
var tmp_stamp = urlParams.get('date');
display_date = new Date(parseInt(tmp_stamp)).toDateString();
}else{
display_date = dataI.toDateString();
}
$("#dateD").html(display_date);
$("#up").click(function(){
if(urlParams.has('date')){
var i = parseInt(urlParams.get('date'))+ 86400000 ;
}else{
var i = current_tm + 86400000 ;
}
var b = new Date(i);
$("#dateD").innerHTML = b.toDateString();
window.location="datechange.php?date="+ i;
});
$("#down").click(function(){
if(urlParams.has('date')){
var i = parseInt(urlParams.get('date')) - 86400000 ;
}else{
var i = current_tm - 86400000 ;
}
var c = new Date(i);
$("#dateD").innerHTML = c.toDateString();
window.location="datechange.php?date="+ i;
});
});