简体   繁体   中英

How to display a date as select tag (only day of the week and date of the month)

I am new to javascript or jQuery, I am trying to place a date picker on my website and I am able to place a datepicker with calendar, but I need it without a calendar and as a select box.

检查示例图片

Any idea how to do it in javascript or jQuery?

Thanks

You probably aren't going to be able to modify datepicker to do this (I will gladly concede to anyone proving otherwise), but you can easily add dates to a standard select control.

In this code, I have used the formatDate from the datepicker UI, so you'll need to include jQuery UI, or at least the datepicker component, for it to work.

The following example adds 10 days, starting with the current day to a select element with an ID of "mySelect".

$(function () {
    var today = new Date();
    var date = new Date();

    for (x = 0; x <= 9; x++) {
        date.setDate(today.getDate() + x);
        textDate = $.datepicker.formatDate('D, dd-M', date);
        $('#mySelect')
            .append($("<option></option>")
            .attr("value", textDate)
            .text(textDate));
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM