jQuery根据日期禁用或启用提交按钮 [英] jQuery disable or enable submit button based on dates

查看:96
本文介绍了jQuery根据日期禁用或启用提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进行了一些搜索,但找不到类似的内容.

I've done some searches but couldn't find anything similar.

我需要根据日期禁用/启用提交"按钮.例如,我需要在周末禁用提交按钮,在工作日启用提交按钮.另外,我需要创建一个弹出窗口或添加一行文本,以表示诸如我们在周末不接受条目"之类的内容.

I need to disable/enable the submit button based on dates. For example, I need the submit button to be disabled during weekends and enabled during weekdays. Also, I need to create either a pop-up or add line of text to say something like "we are not accepting entries during weekends."

谢谢!

推荐答案

这是您的初衷吗?如果是星期六或星期日,则会在按钮上添加disabled类.

Is this what you had in mind? This will add a class of disabled to a button if it's Saturday or Sunday.

$(function() {
  var now = new Date(),
      days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
      day = days[now.getDay()],
      $button = $('#myButton');
  
  if (day === days[0] || day === days[6]) {
      $button.addClass('disabled');
  }
  
  $button.click(function() {
      if ($(this).hasClass('disabled')) {
          alert('We are not accepting entries during weekends.')
          return;
      }
  });
})

.disabled {
  background: grey;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="myButton">Press me only on weekday</button>

这篇关于jQuery根据日期禁用或启用提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆