简体   繁体   English

不允许更改时间React-DatePicker

[英]Don't allow time change React-DatePicker

I'm using the React-DatePicker package along with momentjs to let the user pick a date on an input which looks like this: 我正在使用React-DatePicker包和momentjs,让用户在看起来像这样的输入中选择日期:

在此处输入图片说明

I want to display the time, but not let the user change it directly from the <input> . 我想显示时间,但不希望用户直接从<input>更改时间。 My date picker has an onChange() function: 我的日期选择器具有onChange()函数:

onExpirationDateChange(date) {
   this.setState({selectedTime: date.unix()});
}

Does anyone know how I can achieve displaying the time, but not letting the user change it? 有谁知道我如何实现显示时间,但又不让用户更改时间?

I would recommend this way: 我建议这样:

document.getElementsByTagName("inputdate")[0].setAttribute("readonly", "readonly"); 

As you can problably tell, it's set to readonly, so one can't write in the input anymore but they can still copy the text if needed. 正如您可能会说的那样,它已设置为只读,因此无法再在输入中编写内容,但是如果需要,他们仍然可以复制文本。

As per the examples on this page: https://reactdatepicker.com/ 按照此页面上的示例: https : //reactdatepicker.com/

You might try this to prevent the users from selecting times... 您可以尝试这样做,以防止用户选择时间...

<DatePicker
  selected={this.state.startDate}
  onChange={this.handleChange}
  showTimeSelect
  excludeTimes={[moment().hours(17).minutes(0), moment().hours(18).minutes(30), moment().hours(19).minutes(30)], moment().hours(17).minutes(30)}
  dateFormat="LLL"
/>

Found the answer myself. 自己找到答案。 Just set readOnly on picker and make sure to not include time select option. 只需在选择器上设置readOnly ,并确保不包括时间选择选项即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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