dongse5408 2014-04-20 12:26
浏览 42
已采纳

无法将值分配给php日期框

Hi everyone I'm trying to manually assign a value to an html date input field. But it doesn't work.

First I tried to pass the value the way I do it for ordinary input box

<?php $newDate2 = '03/31/2014'; ?>
<form name='RegForm' method='GET'>
<input type="date" name="sdate" id="sdate" value="<?php echo $newDate2; ?>" required>
</form>

When It didn't work I tried to Use Javascript width something like this:

 <head>
 <script type="text/javascript">
  function showInvoiceDate(){
   var mYDate = new Date('2011-04-11');
   alert(mYDate);
   document.getElementById("sdate").value=mYDate;
 }
 </script>
 </head>
 <body onload="showInvoiceDate();">
  <form name='RegForm' method='GET'>
  <input type="date" name="sdate" id="sdate" required>
  </form>
</body>

The function is being prompted. Because Alert box shows the value of myDate But the value of date box is not being changet its still mm/dd/yyy

So What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douxu5845 2014-04-20 12:51
    关注

    The date should be in the format YYYY-MM-DD. Single digit days and months should be padded with a 0. January is 01.

    From the documentation:

    A string representing a date. Value: A valid full-date as defined in [RFC 3339], with the additional qualification that the year component is four or more digits representing a number greater than 0.

    PHP - code should be:

    <?php $newDate2 = "2014-03-31"; ?>
    <form name='RegForm' method='GET'>
    <input type="date" name="sdate" id="sdate" value="<?= $newDate2; ?>" required>
    </form>
    

    Tested and it works for me.

    Javascript - you need to construct the date string, then set it, like:

    var dateString = mYDate.getUTCFullYear() + "-" + (mYDate.getUTCMonth()+1) + "-" + mYDate.getUTCDate();
    document.getElementById("sdate").value = dateString ;
    

    I didn't test the JS version, but it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c