最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript Checking for empty datepicker - Stack Overflow

programmeradmin1浏览0评论

I've tried every permutation of checking for this logic to work and I cannot find the right syntax.

I have the following bit of Javascript:

var d = $("#myDatepicker1").datepicker("getDate");
if ($.isEmptyObject(d)) {
    $("#dailySummaryDateSelector").datepicker("setDate", new Date());
}

When the page loads and d is evaluated Firebug shows it as Object [ ]

In the if statement I've tried:

if (d == null) {
if (d == {}) {
if (d == Object()) {
if ($.isEmptyObject(d)) {
if (d.isEmptyObject()) {

None of these work. As far as I can tell Object [ ] is an empty object so how do I actually test for that and get the contents of the if statement to execute?

I've tried every permutation of checking for this logic to work and I cannot find the right syntax.

I have the following bit of Javascript:

var d = $("#myDatepicker1").datepicker("getDate");
if ($.isEmptyObject(d)) {
    $("#dailySummaryDateSelector").datepicker("setDate", new Date());
}

When the page loads and d is evaluated Firebug shows it as Object [ ]

In the if statement I've tried:

if (d == null) {
if (d == {}) {
if (d == Object()) {
if ($.isEmptyObject(d)) {
if (d.isEmptyObject()) {

None of these work. As far as I can tell Object [ ] is an empty object so how do I actually test for that and get the contents of the if statement to execute?

Share Improve this question edited May 12, 2013 at 19:33 KingKongFrog 14.4k22 gold badges76 silver badges131 bronze badges asked May 12, 2013 at 19:22 JammerJammer 10.2k11 gold badges73 silver badges122 bronze badges 4
  • 2 Strange. (d == null) should work. You do realize you are calling two ids ($("#myDatepicker1") and $("#dailySummaryDateSelector")), right? – acdcjunior Commented May 12, 2013 at 19:46
  • Well spotted!! I cannot believe I missed that! (d == null) is indeed working now! – Jammer Commented May 12, 2013 at 19:51
  • A datepicker is initialized with the current date as a startdate, so it should'nt normally return null at all, unless you explicitly set it to have no date selected. If the problem is trying to get the date from an object that does'nt exist because you did'nt check your selectors, @acdcjunior should add that as an answer, and you should accept it, as it's well spotted. – adeneo Commented May 12, 2013 at 19:54
  • 1 So who's down voting this? – Jammer Commented May 12, 2013 at 21:09
Add a ment  | 

2 Answers 2

Reset to default 6

getDate()

Returns: Date

Returns the current date for the datepicker or null if no date has been selected. This method does not accept any arguments.

if($("#myDatepicker1").datepicker("getDate") === null) {
  alert("empty");
}

For anyone that missed it my code was broken!

var d = $("#myDatepicker1").datepicker("getDate");
if ($.isEmptyObject(d)) {
    $("#dailySummaryDateSelector").datepicker("setDate", new Date());
}

Should have been:

var d = $("#dailySummaryDateSelector").datepicker("getDate");
if ($.isEmptyObject(d)) {
    $("#dailySummaryDateSelector").datepicker("setDate", new Date());
}

Now this works:

var d = $("#dailySummaryDateSelector").datepicker("getDate");
if (d == null) {
    $("#dailySummaryDateSelector").datepicker("setDate", new Date());
}
发布评论

评论列表(0)

  1. 暂无评论