循环内的时间选择器 [英] Time picker inside a loop for

查看:102
本文介绍了循环内的时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有循环的表格,我想使用一个时间选择器.问题是在循环外部工作正常,但在循环内部却行不通. 对于3输入框,可能会看到时间选择器,但是在选择时间时,它只会更改第一个框.

I have form with a loop and I would like to use a time picker. The problem is outside the loop it works fine but inside the loop it doesn't work. For the 3 input box it's possible to see the time picker but when select the time it will change only the first box.

<?php for ($i = 1; $i <= 3; $i++) { ?>
    Time <input type='text' name='timepicker[<?= $i ?>]' class="datepicker_dynamic" id='timepicker[<?= $i ?>]' value=''/>
<?php } ?>

<script type='text/javascript'>
    $(document).ready(function() { 
        $('.datepicker_dynamic').timepicker({
            showLeadingZero: false,
        });
    });
</script>

推荐答案

我对您的代码做了一些重新排列:

I did some rearranging to your code:

<?php for ($i = 1; $i <= $_SESSION["number"]; $i++) { ?>
    Time
    <input type='text' name='timepicker[<?= $i ?>]' id='timepicker_<?= $i ?>' value=''/>
    <script type='text/javascript'>
        $(document).ready(function() { 
            $('#timepicker_<?= $i ?>').timepicker({
                showLeadingZero: false,
            });
        });
    </script>
<?php } ?>

再次,我同意@Brad的观点,不要不必要地生成这么多JS,这是一个好主意,尤其是当所有日期选择器的功能相同时,您可以执行以下操作以得到相同的结果时:

Again, I agree with @Brad that it's probably a good idea not to be generating this much JS unnecessarily, especially when you could do the following with the same results if all the datepickers are identical in functionality:

<?php for ($i = 1; $i <= $_SESSION["number"]; $i++) { ?>
    Time
    <input type='text' name='timepicker[<?= $i ?>]' class="datepicker_dynamic" id='timepicker[<?= $i ?>]' value=''/>
<?php } ?>

并使用以下JS:

<script type='text/javascript'>
    $(document).ready(function() { 
        $('.datepicker_dynamic').timepicker({
            showLeadingZero: false,
        });
    });
</script>

编辑:最后,问题最终是由于PHP开放标记<?的使用不当而导致的.在此处添加一个小小的解释,以供将来参考:

In the end, the problem ended up being an improper usage of the PHP open tag <?. Adding a small explanation here for future reference:

  • <?php ?>:默认的php打开和关闭标签,默认情况下始终启用
  • <? ?>:php short开放标记.默认情况下可能会禁用此功能,因此请在使用前检查您的PHP设置并启用它.
  • <?= ?>:<?php echo ?>的简写.同样,默认情况下可能会禁用它,因此请在使用前检查您的PHP设置并启用.
  • <?php ?>: The default php open and close tags, always enabled by default
  • <? ?>: The php short open tag. This may be disabled by default, so check your PHP settings and enable this before using it.
  • <?= ?>: shorthand for <?php echo ?>. Again, might be disabled by default, so check your PHP settings and enable before using.

要启用短打标签,请检查您的PHP.ini文件中的short_open_tag

To enable the short open tags, check your PHP.ini file for short_open_tag

这篇关于循环内的时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆