Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

javascript - Input type="date" doesn't work every time I choose a date

I am trying to get date from form with input type="date", when I click submit. Code is written in React. I use Chrome.

<form onSubmit={() => {
  let inp = document.querySelector('.tooltipSetDate').value;
  console.log(inp)
}}>
  <input className="tooltipSetDate" type="date"/>
  <input type="submit" value="+" />
</form>
let inp = document.querySelector('.tooltipSetDate').value;

sometimes I get result in format year-day-month, as I need. But 90% variable inp becomes NaN or Null, or empty string or nothing.

Reloading this page or rerendering a component don't help. I even tried to use npm jQuery, tried to test this code in FireFox, Edge. Also instead of submit I tried to use input onChange. About 1 of 10 times it gives me a correct result.

In simple HTML file this form blinks for a second with right date, then again nothing.

I can not find an error. Please help!

question from:https://stackoverflow.com/questions/66064782/input-type-date-doesnt-work-every-time-i-choose-a-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...