简体   繁体   English

根据下拉菜单中的选定值设置日期时间选择器格式

[英]Set datetimepicker format depending on the chosen value in dropdown

I would like the datetimepicker to show only days if the user chose "Days" in dropdown and show months if the user chose "Months". 如果用户在下拉列表中选择“天”,我希望datetimepicker仅显示天,如果用户选择“月”,则希望显示月。

The code is inside haml file: 该代码位于haml文件中:

= select_tag 'date_format', options_for_select(["Days", "Months"]), onchange: "myFunction()"

= text_field_tag :from, class: 'datepick'

 :javascript
  function myFunction() {
      var value = document.getElementById("date_format").value;
      if (value == "Days") {
        $(".datepick").datepicker("destroy");
        $(".datepick").datetimepicker({
          format: 'd.m.Y'
        });
      } else if (value == "Months") {
        $(".datepick").datetimepicker("destroy");
        $(".datepick").datepicker({
          format: "mm/yyyy"
        });
      }
  }

Right now it doesn't work well. 现在,它不能很好地工作。 Please help me to find out where I made a mistake. 请帮助我找出我犯错的地方。 How to overwrite (".datepick")? 如何覆盖(“ .datepick”)?

尝试使用“ dateFormat”和“ formatDate”选项而不是“ format”

The only option I've found is to create another text_field_tag and hide/show it: 我发现的唯一选择是创建另一个text_field_tag并隐藏/显示它:

= text_field_tag :from, class: 'datepick'
= text_field_tag :from, class: 'datetimepick'

:javascript
  function myFunction() {
      var value = document.getElementById("date_format").value;
      if (value == "Days") {
        $(".datepick").hide();
        $(".datetimepick").show();
        $(".datetimepick").datetimepicker({
          format: 'd/m/Y'
        });
      } else if (value == "Months") {
        $(".datetimepick").hide();
        $(".datepick").show();
        $(".datepick").datepicker({
          format: "mm/yyyy"
        });
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM