我有一个表单,您可以在其中选择一周中的某一天来搜索相应的数据。我希望有一些方法可以在表单加载时预先选择今天的一天。任何指导将不胜感激,我认为这将是javascript,我知道的很少。 乔
<select name="tax_days" id="days-tax" class="gmw-dropdown-days gmw-dropdown-taxonomy">
<option value="0"> – All – </option>
<option class="level-0" value="15">Monday</option>
<option class="level-0" value="29">Tuesday</option>
<option class="level-0" value="5">Wednesday</option>
<option class="level-0" value="43">Thursday</option>
<option class="level-0" value="54">Friday</option>
<option class="level-0" value="57">Saturday</option>
<option class="level-0" value="65">Sunday</option>
</select>
答案 0 :(得分:1)
window.onload = function() {
var currentDay = new Date().getDay();
if (currentDay == 0) {
// Sunday is at the end of the select
currentDay = 7;
}
document.getElementById("days-tax").selectedIndex = currentDay;
};
<select name="tax_days" id="days-tax" class="gmw-dropdown-days gmw-dropdown-taxonomy">
<option value="0">– All –</option>
<option class="level-0" value="15">Monday</option>
<option class="level-0" value="29">Tuesday</option>
<option class="level-0" value="5">Wednesday</option>
<option class="level-0" value="43">Thursday</option>
<option class="level-0" value="54">Friday</option>
<option class="level-0" value="57">Saturday</option>
<option class="level-0" value="65">Sunday</option>
</select>