github.com/xdan/datetimepicker存在问题

时间:2018-01-29 18:53:28

标签: jquery focus datetimepicker

我遇到下面显示的代码问题。

<!DOCTYPE html>
<html>
<head>
<title>
  Test jquery-datetimepicker
</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.17/jquery.datetimepicker.full.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.17/jquery.datetimepicker.css" />
</head>
<body>
  From: <input type=text id=fromDateTime class=stdDateTimePicker name=fromDateTime size=12 value='2018-03-24'>
  <span id=fromDateTimeError></span>
  <br>
  To: <input type=text id=toDateTime class=stdDateTimePicker name=toDateTime size=12 value=''>
</body>
<script>
  $('#fromDateTime').focus();

  $.datetimepicker.setLocale('da');
  $('.stdDateTimePicker').datetimepicker({
    format: 'Y-m-d H:i',
    dayOfWeekStart: 1,
    step: 15
  });

  function strToDate(str) {
    // Test for yyyy-mm-dd
    var d = str.match(/^(\d{4})\-(\d{2})\-(\d{2})$/);
    if (d) {
      var res = new Date(d[1]+'-'+d[2]+'-'+d[3]+'T00:00:00Z');
    } else {
      // Test for yyyy-mm-dd hh:mm
      var dtm = str.match(/^(\d{4})\-(\d{2})\-(\d{2})\ (\d{2})\:(\d{2})$/); 
      if (dtm) {
        var res = new Date(dtm[1]+'-'+dtm[2]+'-'+dtm[3]+'T'+dtm[4]+':'+dtm[5]+':00Z');
      } else {
        // Test for yyyy-mm-dd hh:mm:ss
        var dtms = str.match(/^(\d{4})\-(\d{2})\-(\d{2})\ (\d{2})\:(\d{2})\:(\d{2})$/);
        if (dtms) {
          var res = new Date(dtms[1]+'-'+dtms[2]+'-'+dtms[3]+'T'+dtms[4]+':'+dtms[5]+':'+dtms[6]+'Z');
        } else {
          var res;
        }
      }
    }
    return res;
  }

  $('#fromDateTime').focusout(function() {
    $('#fromDateTimeError').html('');
    var newDateStr = $(this).val();
    var newDate = strToDate(newDateStr);
    if (!newDate) {
      $('#fromDateTimeError').html('Error in date');
      $('#fromDateTime').focus();
    }
  });

</script>
</html>

我希望代码包含字段&#34; From:&#34;当页面显示时,它将保持活动状态,并且在输入有效日期之前应保持活动状态。 我看到的是: 该页面打开,字段为&#34; From:&#34;焦点。 如果我在字段中键入无效日期(字母为fx)并按[Tab],则显示错误文本,内容将替换为datetimepicker和字段&#34; To:&#34指定格式的日期;变得活跃。

如果发现错误,如何禁用datetimepicker?

我如何专注于领域&#34;来自:&#34;只要该字段包含无效内容?

0 个答案:

没有答案