检查DatePicker的值是否为空。

10

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

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


2
DatePicker.SelectedDate == null - Nawed Nabi Zada
3个回答

14

DatePicker类有一个属性SelectedDate,它允许您获取或设置所选日期。如果没有选择日期,则其值将为null。

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

2
尽管DatePicker的默认文本是Select a date,但您可以使用Text属性通过Text.Length进行检查。或者像这样检查SelectedDate属性:
if (datePicker.Text.Length == 0)
{
    //Do your stuff        
}

或者:

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

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

在纯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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接