【问题标题】:tick the checkbox for week days in javascript在javascript中勾选工作日的复选框
【发布时间】:2021-01-21 19:02:05
【问题描述】:

我需要打印具有 3 个名称的复选框。

Person1、Person2、Person3。

Person1 复选框应在星期一选中,person2 在星期二选中,person3 在星期三选中,然后 person 1 在星期四再次选中,就像它应该遵循的那样。是否有任何示例代码可用于完成此操作?

【问题讨论】:

  • 请访问help center,使用tour查看内容和How to Ask。做一些研究,搜索关于SO的相关主题;如果您遇到困难,请发布您的尝试minimal reproducible example,并使用[<>] sn-p 编辑器记录输入和预期输出。
  • person1 是否在周日和周一接受检查?你只有 3 个人所以 "personX,person1,person2,person3,person1,person2,person3"
  • 仅工作日。所以周一的person1,周二的person2,周三的person3,周四的person1,周五的person2,周一的person3..@mplungjan
  • 所以忽略周末?
  • 没错@mplungjan

标签: javascript html css checkbox


【解决方案1】:

类似的东西。

更好的算法是可能的

const start = new Date(2021, 0, 18, 15, 0, 0); // change to 25th when tested
const now = new Date();
now.setHours(15, 0, 0, 0)
const aDay = 24 * 60 * 60 * 1000;
let day;
let person = 0
for (let i = start.getTime(), n = now.getTime(); i <= n; i += aDay) {
  day = new Date(i).getDay()
  if (day!==0 && day !== 6) {
    person++  
    if (person>3) person=1    
  }  
}
if (person>3) person=1;
document.querySelectorAll("#persons .person")[person].checked = true;
<div id="persons">
  1. <input type="checkbox" class="person" name="person1" /><br/> 
  2. <input type="checkbox" class="person" name="person2" /><br/> 
  3. <input type="checkbox" class="person" name="person3" /><br/>
</div>

【讨论】:

    【解决方案2】:

    这将使其每周轮换,但正如我所提到的,我建议在后端以某种方式对其进行跟踪,因此它不是超级静态的,而是烧录在代码中的。

    let day = new Date().getDay();
    let now = new Date();
    let onejan = new Date(now.getFullYear(), 0, 1);
    let week = Math.ceil( (((now.getTime() - onejan.getTime()) / 86400000) + onejan.getDay() + 1) / 7 );
    
    let rotationFlag = week % 3;
    let firstPerson = 0;
    
    if(rotationFlag === 1) {
      firstPerson = 1;
    } else if (rotationFlag === 2) {
      firstPerson = 2;
    } else {
      firstPerson = 3;
    }
    
    if(day === 1 | day === 4) {
      document.getElementById("person" + firstPerson).checked = true;
    } else if(day === 2) {
      document.getElementById("person" + firstPerson + 1).checked = true;
    } else {
      document.getElementById("person" + firstPerson + 2).checked = true;
    }
    <input type="radio" name="person" id="person1" />
    <label for="person1">person1</label>
    <br>
    
    <input type="radio" name="person" id="person2" />
    <label for="person2">person2</label>
    <br>
    
    <input type="radio" name="person" id="person3" />
    <label for="person3">person3</label>
    <br>

    【讨论】:

    • 这里没有什么可以做这个改变 123,123,123
    • 只是好奇,在这种情况下 -> 输出将是 person1 - thursday, person2- friday, person 3 - monday 这样吗?对吗?
    • 是的,应该是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2017-10-09
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2014-03-12
    • 2022-11-03
    相关资源
    最近更新 更多