dongshiqin1352 2018-06-14 15:19
浏览 101

在PHP中循环并合并Datetimepicker

I'm using datetimepicker javascript for bootstrap. The instance works if there is only one input field or if it's the first of multiple. But if there are multiple fields, the others don't work.

The widget is created from a foreach loop in php so foreach record it gives a new section with title, expiration date and a datetimepicker field to create a new date. However, if I hit the calendar icon of the 2nd or 3rd datepicker item it just activates the popover calendar for the first one.

How can i properly incorporate this into the loop so that each field has it's own input for datetimepicker?

<?php foreach($v as $k=>$n): ?>
<!--Body of the Modal for expired displays-->
<br>
<h6><?php echo $n['Type'] ?></h6>
<h6>Date/Time Expiring: <?php echo $n["Expiration"] ?></h6>
<h6>Set New Expiration:</h6>
  <div class="form-group">
    <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
      <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1"/>
      <span class="input-group-addon" data-target="#datetimepicker1" data-toggle="datetimepicker">
          <span class="fa fa-calendar"></span>
      </span>
    </div>
  </div>                 
<?php endforeach; ?>

<script type="text/javascript">
$(function () {
  $('#datetimepicker1').datetimepicker({
     icons: {
                  time: "fa fa-clock-o",
                  date: "fa fa-calendar",
                  up: "fa fa-arrow-up",
                  down: "fa fa-arrow-down"
              }
  });
});
</script>

UPDATE:

<div class="form-group">
              <div class=" datepicker input-group date"  data-target-input="nearest">
                <input type="text" class="form-control datetimepicker-input" data-target=".datepicker"/>
                <span class="input-group-addon" data-target=".datepicker" data-toggle="datetimepicker">
                    <span class="fa fa-calendar"></span>
                </span>
              </div>
            </div>
<script type="text/javascript">
$('.datepicker').each(function(){
  $(this).datetimepicker();
   icons: {
                time: "fa fa-clock-o",
                date: "fa fa-calendar",
                up: "fa fa-arrow-up",
                down: "fa fa-arrow-down"
            }
  });

</script>
  • 写回答

2条回答 默认 最新

  • dongyu4908 2018-06-14 15:34
    关注

    You have several div's with the same ID. This is not correct.

    <?php foreach($v as $k=>$n): ?>
    <!--Body of the Modal for expired displays-->
    <br>
    <h6><?php echo $n['Type'] ?></h6>
    <h6>Date/Time Expiring: <?php echo $n["Expiration"] ?></h6>
    <h6>Set New Expiration:</h6>
      <div class="form-group">
        <div class="input-group date datetimepicker1" data-target-input="nearest">
          <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1"/>
          <span class="input-group-addon" data-target="#datetimepicker1" data-toggle="datetimepicker">
              <span class="fa fa-calendar"></span>
          </span>
        </div>
      </div>                 
    <?php endforeach; ?>
    
    <script type="text/javascript">
    $(function () {
      $('.datetimepicker1').datetimepicker({
         icons: {
                      time: "fa fa-clock-o",
                      date: "fa fa-calendar",
                      up: "fa fa-arrow-up",
                      down: "fa fa-arrow-down"
                  }
      });
    });
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站