簡體   English   中英

勾選 javascript 中的工作日復選框

[英]tick the checkbox for week days in javascript

我需要打印具有 3 個名稱的復選框。

人 1、人 2、人 3。

Person1 復選框應該在星期一、person2 在星期二、person3 在星期三和 person 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>

這將使其每周輪換,但正如我所提到的,我建議在后端以某種方式對其進行跟蹤,因此它不是超級 static,在代碼中燒錄。

 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM