我想在 html 表單中創建一個下拉串列,它將動態列出接下來的四個即將到來的星期六。這個想法是,當有人注冊時,他們可以選擇他們想在即將到來的四個星期六中的哪一個來訪問我們的空間,這樣我們就可以跟蹤并發送自動電子郵件回復。每周的時間都一樣,所以這不是問題。
一直在嘗試通過 Wordpress 插件來做到這一點,但事實證明,與電子郵件軟體集成起來很復雜……我認為在 html/javascript/php 中做到這一點一定更容易、更清晰。
uj5u.com熱心網友回復:
<?
echo '<label for="saturday">Choose which Saturday:</label>';
echo "<select id=\"saturday\" >\n";
$saturday = (6 - date('w',time())) ; // how many days to Saturday
$saturday = time() ($saturday * 86400); // number of seconds to Saturday
$value = date('Y-m-d',$saturday); // Get proper form date format
$text = date('D M jS',$saturday); // Get human readable format
echo "<option value=\"$option\">$text</option>\n";
for ($x = 1; $x < 4 ; $x ) {
$saturday = 604800;
$option = date('m-d-y',$saturday) ;
$text = date('D M jS',$saturday);
echo "<option value=\"$option\">$text</option>:\n";
}
echo '</select>';
?>

uj5u.com熱心網友回復:
請檢查以下示例是否可以用于您的要求。我使用了一個按鈕,這樣您就可以看到發生了什么,并且您可能會為您的應用程式使用不同的事件,例如 onl oad。
function myFunction() {
let dateToday = new Date();
let dateCopy = new Date(dateToday.getTime());
let mySelectID = document.getElementById("mySaturdays");
for (let x = 1; x < 5; x ) {
const nextSaturday = new Date(
dateCopy.setDate(
dateCopy.getDate() ((7 - dateCopy.getDay() 6) % 7 || 7),
),
);
let option = document.createElement("option");
option.value = x;
option.text = nextSaturday;
mySelectID.add(option);
}
}
<form>
<select id="mySaturdays" size="5">
</select>
</form>
<br>
<button type="button" onclick="myFunction()">Insert option</button>
uj5u.com熱心網友回復:
這是一個例子
請注意通用的 nearestDay 以獲得從今天到下周今天的一周中的任何一天
const nearestDay = dayOfWeek => {
let now = new Date();
now.setDate(now.getDate() (((dayOfWeek 7 - now.getDay()) % 7) || 7));
return now;
};
const dates = () => {
let date = nearestDay(6); // Saturday
const arr = [];
while (arr.length < 4) {
arr.push(date.toISOString().split("T")[0])
date.setDate(date.getDate() 7)
}
return arr;
}
const dateArr = dates();
document.getElementById("saturdays").innerHTML = dateArr.map(date => `<option value="${date}">${date}</option>`).join("");
<select id="saturdays">
<option value="">Please select</option>
</select>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/535700.html
