检查DatePicker值是否为null

时间:2022-11-15 17:07:32

I would like to check if the value of a DatePicker is null (== no date in the box).

我想检查DatePicker的值是否为null(==框中没有日期)。

By default the Text of my DatePicker is set to something like Select a date, so I can't use the Text property to check.

默认情况下,我的DatePicker的Text设置为类似于选择日期,因此我无法使用Text属性进行检查。

3 个解决方案

#1


6  

The DatePicker class has a property, SelectedDate, which enable you to get or set the selected date. If there is none selected, it means its value will be null.

DatePicker类有一个属性SelectedDate,可用于获取或设置所选日期。如果没有选择,则表示其值为null。

if (datePicker.SelectedDate == null)
{
    // Do what you have to do
}

#2


2  

Although the default text of the DatePicker is Select a date but you can use the Text property to check by using Text.Length. Or check SelectedDate property like this:

虽然DatePicker的默认文本是选择日期,但您可以使用Text.Length来检查Text属性。或者像这样检查SelectedDate属性:

if (datePicker.Text.Length == 0)
{
    //Do your stuff        
}

Or:

if (datePicker.SelectedDate == null)
{
    //Do your stuff
}

#3


0  

to = $('#to');// hopefully you defined this yourself and the options
//I am writing coffeeScript here, below I will add pure JS

if to.datepicker('getDate') == null
  //basically do your stuff here - 
  //using month_day_year_string (m_d_y_s) func to format a date object here and add 6 to the date's year so passed e.g. 2014 gives 2020.
  return to.datepicker('option', 'minDate', getDate(this), to.datepicker('option', 'maxDate', m_d_y_s(getDate(this), 6)))
return// I have this on a call back so I am returning from the call back

In pure JS it would simply be

纯粹的JS就是这样

var to = $('#to');// hope you defined the options and datepicker
if (to.datepicker('getDate') === null) {
  #do your stuff - I have an example code below as I needed this check in a callback
  return to.datepicker('option', 'minDate', getDate(this), to.datepicker('option', 'maxDate', m_d_y_s(getDate(this), 6)));
 }

return;// if on a call back e.g. onChange or onRender.

#1


6  

The DatePicker class has a property, SelectedDate, which enable you to get or set the selected date. If there is none selected, it means its value will be null.

DatePicker类有一个属性SelectedDate,可用于获取或设置所选日期。如果没有选择,则表示其值为null。

if (datePicker.SelectedDate == null)
{
    // Do what you have to do
}

#2


2  

Although the default text of the DatePicker is Select a date but you can use the Text property to check by using Text.Length. Or check SelectedDate property like this:

虽然DatePicker的默认文本是选择日期,但您可以使用Text.Length来检查Text属性。或者像这样检查SelectedDate属性:

if (datePicker.Text.Length == 0)
{
    //Do your stuff        
}

Or:

if (datePicker.SelectedDate == null)
{
    //Do your stuff
}

#3


0  

to = $('#to');// hopefully you defined this yourself and the options
//I am writing coffeeScript here, below I will add pure JS

if to.datepicker('getDate') == null
  //basically do your stuff here - 
  //using month_day_year_string (m_d_y_s) func to format a date object here and add 6 to the date's year so passed e.g. 2014 gives 2020.
  return to.datepicker('option', 'minDate', getDate(this), to.datepicker('option', 'maxDate', m_d_y_s(getDate(this), 6)))
return// I have this on a call back so I am returning from the call back

In pure JS it would simply be

纯粹的JS就是这样

var to = $('#to');// hope you defined the options and datepicker
if (to.datepicker('getDate') === null) {
  #do your stuff - I have an example code below as I needed this check in a callback
  return to.datepicker('option', 'minDate', getDate(this), to.datepicker('option', 'maxDate', m_d_y_s(getDate(this), 6)));
 }

return;// if on a call back e.g. onChange or onRender.