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

时间:2014-04-22 03:54:31

标签: javascript jquery html

如果当前日期是3/11/14并且用户选择星期三然后3月12日,3 / 应该打印19 / 14,3 / 26 / 14,4 / 2 / 14,1,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());
  });

2 个答案:

答案 0 :(得分:0)

我创建了new demo

使用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;
}

您可以研究如何在here

使用date对象

答案 1 :(得分:0)

试试这个。 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;
            }
        }
    });
});