【问题标题】:Show only today's and upcoming days' dates to select in html input type date [duplicate]仅显示今天和未来几天的日期以在 html 输入类型日期中选择 [重复]
【发布时间】:2021-10-27 14:15:22
【问题描述】:
<label class="whitetext">Select Date:</label>
<input type="date" class="form-control" name="pickup_date" required>

我的 html 文件中有这个输入字段。我想让它使用户只能选择从今天开始的日期,而不是过去的日期。 我认为可以使用min 来完成,但我不知道如何将min 设置为今天的日期。

示例-->如果今天是 2021 年 10 月 27 日,则仅接受 27 日和所有未来日期作为输入,或者过去的日期被取消或无法选择等。

【问题讨论】:

标签: javascript html


【解决方案1】:

您可以使用 setAttribute 来通过 javascript 设置最小值。

let today = new Date()

let year = today.getFullYear()
let month = today.getMonth() + 1 // the months are indexed starting with 0
let date = today.getDate()

let dateStr = `${year}-${month}-${date}`

let input = document.querySelector('[name=pickup_date]')
input.setAttribute('min', dateStr)

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    相关资源
    最近更新 更多