回调函数代码拒绝按预期在我的控制台上呈现...可能是什么问题?
FormEl = document.getElementById ("form");
FormEl.addEventListener( "Submit",() =>{
Const userInput = inputNum.value
Console.log (userInput)
})
我希望在单击 html 上的提交按钮后返回输入到输入字段元素中的值。
// Assume you have an input element with id="inputNum"
const formEl = document.getElementById("form");
const inputNum = document.getElementById("inputNum");
formEl.addEventListener("submit", (event) => {
// Prevent the default form submission behavior
event.preventDefault();
// Get the value from the input field
const userInput = inputNum.value;
// Log the input value to the console
console.log(userInput);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Example</title>
</head>
<body>
<form id="form">
<input type="text" id="inputNum" placeholder="Type something">
<button type="submit">Submit</button>
</form>
<script src="your-script.js"></script>
</body>
</html>
将“your-script.js”替换为 JavaScript 文件的路径。通过这些更正,单击提交按钮现在应该将输入值记录到控制台,而无需重新加载页面。 你可以用它。