如何获得日期值与onChange?



所以我有这个代码

日期选择器

<input
type='date'
id='sDate'
onChange={functionSDateChange}
/>

函数functionSDateChange: () => console.log('Start date changed to' + document.getElementById('sDate'))

我希望输出是我选择的日期,但是console.log只显示以下内容:Start date changed to[object HTMLInputElement]

你能告诉我获取日期值onChange的正确方法吗?

我已经试过了:

const val = document.getElementById('sDate')
functionSDateChange: () => console.log('Start date changed to' + val)

但它显示如下:Start date changed tonull

按照您的示例,您可以这样做:

<input
type='date'
id='sDate'
onChange={(val) => functionSDateChange(val)} // val is the new value after change
/>

之后你可以像这样声明函数:

const functionSDateChange: (val: TypeOfValue) => void = (val) => {
console.log('This is the new value ' + val);
}

希望有帮助!如果您需要更多的型号或其他方面的解释,请告诉我。无论如何,你可以在这里找到更多关于onChange的信息:

最新更新