簡體   English   中英

我正在嘗試使用表單選擇選項獲取當前日期,並在提交后顯示接下來的7天

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

如果當前日期是2014年3月11日,並且用戶選擇了星期三,則3/12 / 14、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());
  });

我創建了一個新的演示

使用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對象

試試這個。 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;
            }
        }
    });
});

暫無
暫無

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

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