dongxun2903 2014-10-10 12:32
浏览 51
已采纳

如何在jQuery中将PHP变量传递给Datepicker?

I'm trying to set the default date in datepicker with a variable I pass into the html from PHP. Here's a simplified version of both my control file and form:

control file:

<?php
  function render($template, $values = []) {        
      // extract variables into local scope
         extract($values);           

      // render template
         require("$template");
}

  $default_date = date("m/d/Y") ;       
  $default_date = strtotime($default_date);
  $default_date = $default_date + 604800;
  $default_date = date("Y,m-1,d",$default_date);
  render("index_month2_form.php",['default_date'=> $default_date]);
?>

and here is the form:

<!doctype html>
<html lang="en">
 <head>
  <?php print "$default_date"; ?> 
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="jqueryui/css/swanky-purse/jquery-ui-1.10.4.custom.css">
  <script src="/jqueryui/jquery-1.11.1.min.js"> </script>
  <script src="jqueryui/js/jquery-ui-1.10.4.custom.min.js"></script>  
  <script type="text/javascript">
      $(function() { 
       $("#mydate").datepicker ({
          onSelect: function(dateText, inst) {
                 var dateAsString = dateText;     
                 var date = $('#mydate').val();       
          }
       }) 
       //.datepicker("setDate",new Date());  
       .datepicker("setDate",new Date(2014,10-1,17));  
      });  
  </script>   
 </head>
 <body> 
  <p>Date: <input type="text" id="mydate"></p> 
 </body>
</html>

If I use the commented line for setDate I get the current date. If I use the line I have I get the date 7 days forward. When I print $default_date at the top of the form I get 2014,10-1,17 but I can't firgure out of way to pass this into the script. Others have suggested using

  • 写回答

6条回答 默认 最新

  • douzhantanju1849 2014-10-10 12:58
    关注

    using echo is the way to go.
    I would rather put it in the script rather than the input
    I assume $default_date === "2014,10-1,17"

    $(function() { 
           $("#mydate").datepicker ({
              onSelect: function(dateText, inst) {
                     var dateAsString = dateText;     
                     var date = $('#mydate').val();       
              }
           }) 
           //.datepicker("setDate",new Date());  
           .datepicker("setDate",new Date(
    <?php
         $date = explode(',', $default_date);
         echo $date[0] .  ',' . $date[1] . ',' . $date[2]; 
    ?>
         ));  
    };  
    

    edit:
    As rss81 noticed it, destructuring the string to rebuild the exact same string is quite dumb. I dont know what I was thinking...
    Nevertheless I'll let it like this for educational purpose.

    exploding the string enables us to get an array of the string of each chunk separated by a coma. You could use it to reorder the string. For instance if you wanted to transform "2014,10-1,17" to "10-1,2014,17" that would be done by echo $date[1].','.$date[0].','.$date[2]

    Echo enables us to output the html page as we like, making it dynamic. So here we are preprocessing the date argument of the .datepicker() by php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵