我正在尝试使用表单选择选项获取当前日期,并在提交时显示接下来的七天

I am trying to get current date with form select option and display next seven days on submit

本文关键字:提交 接下来 七天 当前日期 显示 选项 选择 表单 获取      更新时间:2023-09-26

如果当前日期是2014年11月3日,并且用户选择了星期三,则为2014年12月3日/应打印19/14、3/26/14、4/2/14、4/9/14、4/16/14。

HTML

<div class="form-group">
                                        <select  id="selectDay" placeholder="Favorite day of the week"> 
                             <option selected="selected" ></option>
                             <option value="1">Monday</option>
                             <option value="2">Tuesday</option>
                             <option value="3">Wednesday</option> 
                             <option value="4">Thursday</option>
                             <option value="5">Friday</option>
                             <option value="6">Saturday</option>
                             <option value="7">Sunday</option>
                            </select>
                                      </div>
JAVASCRIPT
$(document).ready(function(){
    var CurrentDate=new Date();
    $("#selectDay").val(CurrentDate.getDate());
  });

试试这个。http://jsfiddle.net/dfc8e/8/

$(document).ready(function () {
    $("#selectDay").change(function () {
        var day = parseInt($("#selectDay").val());
        var CurrentDate = new Date();
        var offset = CurrentDate.getDay() < day ? 0 : 7;
        var addDays = day + offset - CurrentDate.getDay();
        for (var i = 0; i < 4; i++) {
            CurrentDate.setDate(CurrentDate.getDate() + addDays);
            alert(CurrentDate);
            if (i == 0) {
                addDays = 7;
            }
        }
    });
});

我创建了一个新的演示

使用date对象,您应该可以通过自己的打印您想要的内容

function show(){
var list = document.getElementById("selectDay");
var day = parseInt(list.options[list.selectedIndex].value); //you would need to parse it to int if you want to do math
var currentDate = new Date().getDate();
var tick = new Date().setDate(currentDate+day); // this will give you the last date
var d = new Date(tick);
var x = document.getElementById("demo");
x.innerHTML=d;
}

您可以在这里学习如何使用date对象