JavaScript 验证的跨浏览器问题
所以我的代码在 Chrome 中运行得很好。在 FF 中,我收到带有日期的前两个警报,它们的格式正确,但如果选择过去的日期,则不会引发第三个警报。在 IE 中,即使我选择未来的日期,我也会收到所有三个警报。正如我提到的,Chrome 可以正常工作,因此,如果有人能够提供一些见解,使其能够在所有浏览器上工作,那就太好了。
function check()
{
var selecteddate = document.form1.selectmonth.value + "-" + document.form1.selectday.value + "-" + document.form1.selectyear.value;
var d = new Date();
var today = (d.getMonth()+1) + "-" + d.getDate() + "-" + d.getFullYear();
// I'd then make some alerts and it'd return the selected date and today with no problem
alert(selecteddate);
alert(today)
//Now for the if statement is where it just stops working. I think maybe I'm doing
//something wrong just solely in the if statement.
if(new Date(selecteddate) < new Date(today))
{
alert("Past dates are not valid");
return false;
}
}
</script>
So I got my code to work just fine in Chrome. In FF I get the first two alerts with the dates and they're formatted properly and everything but if a past date is chosen it doesn't throw the third alert. In IE I get all three alerts but even if I choose a future date. As I mentioned Chrome works as it should so if anyone can provide some insight into making it work across them all that'd be great.
function check()
{
var selecteddate = document.form1.selectmonth.value + "-" + document.form1.selectday.value + "-" + document.form1.selectyear.value;
var d = new Date();
var today = (d.getMonth()+1) + "-" + d.getDate() + "-" + d.getFullYear();
// I'd then make some alerts and it'd return the selected date and today with no problem
alert(selecteddate);
alert(today)
//Now for the if statement is where it just stops working. I think maybe I'm doing
//something wrong just solely in the if statement.
if(new Date(selecteddate) < new Date(today))
{
alert("Past dates are not valid");
return false;
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的日期对 FF 无效,请使用
"/"
代替selecteddate
和today
的"-"
。或者只使用
而不是
your dates are invalid to FF, use
"/"
in place of the"-"
forselecteddate
andtoday
.or what about just using
instead of