【问题标题】:Having to manually define number of datepickers in HTML必须在 HTML 中手动定义日期选择器的数量
【发布时间】:2015-08-24 14:16:02
【问题描述】:

所以我的 Grails 应用程序中有一个工作表单,其中有 4 个 <td><g:textField id="datepicker" .../></td> 实例,但我必须手动将 id 定义为

...
<td><g:textField id="datepicker1" .../></td>
...
<td><g:textField id="datepicker2" .../></td>
...
<td><g:textField id="datepicker3" .../></td>
...
<td><g:textField id="datepicker4" .../></td>
...

HTML 的头部(在我的 GSP 中)包含

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script type="text/javascript">
  $(function() {
    $("#datepicker1").datepicker();
    $("#datepicker2").datepicker();
    $("#datepicker3").datepicker();
    $("#datepicker4").datepicker();
  });
</script>

有没有一种方法可以做到这一点,我不必预先定义页面上有多少个日期选择器,将datepicker# 分配给每个,它只会弹出日历,无论哪个日期文本我点击的框?

在头脑中只使用一个#datepicker 并尝试在每个上都使用&lt;td&gt;&lt;g:textField id="datepicker" .../&gt;&lt;/td&gt; 目前不起作用。

【问题讨论】:

  • 使用类代替,即。 class="datepicker" 和 jquery $(".datepicker").datepicker();
  • 中提琴!非常感谢!像魅力一样工作

标签: javascript jquery html grails datepicker


【解决方案1】:

添加类或数据属性来标识元素,而不是传递 ID

<td><g:textField class="datepicker" .../></td>
<td><g:textField class="datepicker" .../></td>

<td><g:textField data-datepicker .../></td>
<td><g:textField data-datepicker .../></td>

然后

<script type="text/javascript">
    $(function() {
        $(".datepicker").datepicker(); //searchs all elements with class datepicker

        //or

        $("[data-datepicker]").datepicker(); //searchs for all elements with attribute data-datepicker
    });
</script>

【讨论】:

  • 我更喜欢数据属性以避免 CSS 类冲突
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
相关资源
最近更新 更多