Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Date.parse 2.0几乎可以理解它_Javascript_Date - Fatal编程技术网

Javascript Date.parse 2.0几乎可以理解它

Javascript Date.parse 2.0几乎可以理解它,javascript,date,Javascript,Date,它现在可以工作,但不需要按回车键(enter)。我必须感谢您在我能理解的地方写了这篇文章。部分问题在于e未定义为处理程序的参数 window.onload = init; function init() { var button = document.getElementById("addButton"); button.onclick = handleButtonClick; } function handleButtonClick(e) { var textInput = documen

它现在可以工作,但不需要按回车键(enter)。我必须感谢您在我能理解的地方写了这篇文章。

部分问题在于
e
未定义为处理程序的参数

window.onload = init;

function init() {
var button = document.getElementById("addButton");
button.onclick = handleButtonClick;
}

function handleButtonClick(e) {
var textInput = document.getElementById("dateTextInput");
var dateString = textInput.value;
var parsedDate = Date.parse(dateString);
var dateValue = new Date(parsedDate);
var valid = !isNaN(dateValue);

if (!valid) {
    alert("Please enter a valid date");
} else {
    alert(dateValue);
}
e.preventDefault();
}
}
确保
e
是已定义的参数:

function handleButtonClick() {
    e.preventDefault(); // e is undefined and this line will not work
}
要获取解析的日期,只需使用该变量:

function handleButtonClick(e) {
    e.preventDefault(); // e is now defined and will work as expected
}

这里有一个小提琴可以帮助澄清这一点:

只使用JavaScript,而不使用jQueryI注意到了这一点。只需将参数
e
传递到函数中即可。@user2445344:在输入框中按enter键将
提交
,而不是单击按钮。你将需要处理一个不同的事件。仍然不工作,这太烦人了,我甚至不确定我是否需要e.preventDefault。这是上一次的暗示,它现在起作用了。还有一个小问题。按enter键时,它将不起作用,只需单击enter按钮。代码更新了谢谢你,皮特。以后我将跳过onKeyPress的内容。@user2445344:如果您将
addButton
更改为
并添加
文档。表单[0]。提交=handleButtonClick
init()
,它还将处理Enter按钮。更新小提琴:
alert(dateValue);