我对编程很新(从昨天开始)到目前为止我刚刚创建了一个非常简单的代码。我目前不知道如何制作它所以我可以点击回车,它将提交论坛帖子,所以任何关于如何做到这一点的命令将不胜感激:)
<!DOCTYPE html>
<html>
<head>
<title>Title Changing Simulator!</title>
<script type="text/javascript">
function substitute() {
var myValue = document.getElementById('myTextBox').value;
if (myValue.length == 0) {
alert('Please enter a real value in the text box!');
return;
}
var myTitle = document.getElementById('title');
myTitle.innerHTML = myValue;
}
</script>
</head>
<body>
<h1 id="title">Title Changing Simulator!</h1>
<input type="text" id="myTextBox" />
<input type="submit" value="Click My Button" onclick="substitute()" />
</body>
</html>
答案 0 :(得分:0)
将您的输入和按钮换入<form>
并听取表单的submit事件,该事件可通过单击提交按钮或在输入字段中按Enter键来触发。另请参阅How to trigger enter key press of textbox?
const form = document.getElementById('form');
const text = document.getElementById('text');
const title = document.getElementById('title');
form.addEventListener('submit', event => {
event.preventDefault();
if (text.value.length > 0) {
title.textContent = text.value;
} else {
alert('Please enter a real value in the text box!');
}
});
<h1 id="title">Title Changing Simulator!</h1>
<form id="form">
<input type="text" id="text">
<input type="submit" value="Update">
</form>