首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果输入日期小于4个月,则禁用按钮。

如果输入日期小于4个月,则禁用按钮。
EN

Stack Overflow用户
提问于 2017-11-08 08:08:26
回答 6查看 1.5K关注 0票数 2

当输入日期小于4个月时,我想禁用next按钮。获取当前日期并检查当前日期是否小于4个月。如果它较小,请禁用next按钮并发出警报。

我尝试使用一个警报按钮来测试数据报警器,但这没有起作用:

代码语言:javascript
运行
复制
jQuery(document).ready(function($) {
  $('#input_18_104').datepicker({
    onSelect: function() {
      var date = $(this).datepicker('getDate');
      var today = new Date();
      if ((new Date(today.getFullYear(), today.getMonth(), today.getDate() + 120)) < date) {
        //Do somthing here..
        alert(123);
      }

    },
  });
});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.css" rel="stylesheet" />

<input name="input_104" id="input_18_104" type="text" class="datepicker medium mdy datepicker_no_icon hasDatepicker" tabindex="72" placeholder="Date?"> Next button:

<input type="button" id="next_button_18_94" class="form_next_button button" value="Next" tabindex="76">

EN

Stack Overflow用户

发布于 2017-11-08 08:49:39

我会根据它们的时间值来比较日期。这并不是100%的准确,因为不是所有的月份都有30天,但没问题。下面是一个简单的尝试

代码语言:javascript
运行
复制
jQuery(document).ready(function ($) {
    $('#input_18_104').datepicker({
    onSelect: function(){
             var date = $(this).datepicker('getDate');
             var today = new Date();
             var threshold = 10368000000; // 120d in ms = 4*30*24*60*60*1000
             var d = today.getTime()-date.getTime();
             //console.log(d, d > threshold)
             
             // be optimistic...
             $('#next_button_18_94').attr('disabled', false);
             
             if (d < 0) {
                 // selected date is in the future => everything ok
                 return;
             }
             if (d > threshold) {
                 // oops, selected date more than 4 months in the past
                 $('#next_button_18_94').attr('disabled', true);
                 return;
             }
             
        },
    });
});
代码语言:javascript
运行
复制
#next_button_18_94[disabled]{
  border: 2px solid red;
}
代码语言:javascript
运行
复制
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<input name="input_104" id="input_18_104" type="text" tabindex="72" >
<input type="button" id="next_button_18_94" class="form_next_button button" value="Next" tabindex="76">

票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47174515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档