简体   繁体   English

如何动态更改jquery datepicker样式

[英]How to change jquery datepicker style dynamically

I'm struggling with a jquery datepicker which changes style dynamically. 我正在努力与动态更改样式的jquery datepicker。 What I want to do is, if checkbox is clicked then datepicker shows only year and month, if not, datepicker works as normal with year, month and date. 我想做的是,如果单击复选框,则datepicker仅显示年份和月份,否则,datepicker正常显示年份,月份和日期。

I wrote below script. 我写了下面的脚本。 I could see below script was triggered and didn't show any problem, but it didn't work. 我可以看到下面的脚本被触发了,没有显示任何问题,但是没有用。

If anyone knows what the problem is, please help me. 如果有人知道问题出在哪里,请帮助我。 That would be great, thank you. 太好了,谢谢。

===== edit ===== =====编辑=====

With Barmar's advice, I could make this working to change dateFormat dynamically. 根据Barmar的建议,我可以使它动态地更改dateFormat。 But I still can't toggle displaying calendar part of datepicker which I'm trying with $(".ui-datepicker-calendar").css({ "display": "none" }); 但是我仍然无法切换要使用$(".ui-datepicker-calendar").css({ "display": "none" }); and $(".ui-datepicker-calendar").css({ "display": "inline-block" }); $(".ui-datepicker-calendar").css({ "display": "inline-block" });

Does anybody know the solution for this? 有人知道解决方案吗?

function addEventCheckBox() {
    $("#checkBoxForFlag").live("click", function(e) {
        changeDatePicker();
    });
}

function changeDatePicker() {
    var flag = $("#checkBoxForFlag").is(":checked");

    if (flag) {
        $("#testDate").datepicker("option", {
            dateFormat: "yy/mm"
        });

        $(".ui-datepicker-calendar").css({ "display": "none" });
    } else {
        $("#testDate").datepicker("option", {
            dateFormat: 'yy/mm/dd',
        });

        $(".ui-datepicker-calendar").css({ "display": "inline-block" });
    }
}

The .datepicker(<options>) function can only be used to initialize a new datepicker. .datepicker(<options>)函数只能用于初始化新的日期选择器。 To change an existing datepicker, use the option method. 要更改现有的日期选择器,请使用option方法。 You can give either an object containing the options you're changing, or just one option as a single argument. 您可以给一个对象包含要更改的选项,也可以只给一个选项作为单个参数。

function changeDatePicker() {
    var flag = $("#checkBoxForFlag").is(":checked");

    if (flag) {
        $("#testDate").datepicker("option", "dateFormat", "yy/mm");
        $(".ui-datepicker-calendar").hide();
    } else {
        $("#testDate").datepicker("option", "dateFormat", "yy/mm/dd");
        $(".ui-datepicker-calendar").show();
    }
}

After some experimental tries, I solved this issue. 经过一些实验性尝试,我解决了这个问题。 So I'm writing the solution here for someone who might have same problem. 因此,我在这里为可能有相同问题的人编写解决方案。

function changeDatePicker() {
  var flag = $("#checkBoxForFlag").is(":checked");

  if (flag) {
    $("#testDate").datepicker("destroy");
    $("#testDate").datepicker({
      showOn: "both",
      buttonImageOnly: false,
      dateFormat: "yy/mm",
      changeMonth: true,
      changeYear: true,
      maxDate: 0,
      buttonText: "",
      onClose: function () {
        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
        $(this).datepicker('setDate', new Date(year, month, 1));
      }
    });
  $("#testDate").focus(function () {
    $(".ui-datepicker-calendar").hide();
    $("#ui-datepicker-div").position({
        my: "center top",
        at: "center bottom",
        of: $(this)
    });
  });
  $("#testDate").blur(function () {
    $(".ui-datepicker-calendar").hide();
  });
  } else {
    $("#testDate").datepicker("destroy");
    $("#testDate").datepicker({
      showOn: "both",
      buttonImageOnly: false,
      dateFormat: userDateFormat_datepicker,
      changeMonth: true,
      changeYear: true,
      showButtonPanel: true,
      maxDate: 0,
      buttonText: ""
    });
    $("#testDate").focus(function () {
      $(".ui-datepicker-calendar").show();
    });
    $("#testDate").blur(function () {
      $(".ui-datepicker-calendar").show();
    });
  }
}

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

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