我想在没有 PHP 的情况下使用‹input›,因为我的代码编辑器 (Glitch.com) 不能很好地与 PHP 配合使用。有没有办法在没有服务器端代码的情况下使用‹input›?示例:以某种方式将输入存储为变量。这是不正确的,我知道,但我可以这样做吗?
<form>
<label for="FirstName">First name:</label>
var input = <input type="text" id="fname" name="fname">
</form>
然后使用 getElementById 并用输入变量替换文本。
我知道我可以使用 prompt(),但我不想弹出一个窗口,而是这样的:
<form>
<label for="Username">Enter New Name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="Submit" value="Submit">
</form>
如果你能帮上忙,
谢谢!
更新: 我想要完成的是我正在制作一个记分员,我希望用户能够重命名自己。对不起,如果我不清楚。
答案 0 :(得分:1)
您绝对可以使用 Javascript 实现这一点:
const firstName = document.querySelector('#fname');
const lastName = document.querySelector('#lname');
const submit_button = document.querySelector('input[type="button"]');
const heading = document.querySelector('h1')
submit_button.addEventListener('click', () => {
heading.textContent = "Last Name: " + lastName.value;
})
<h1 class="display-first-name"></h1>
<form>
<label for="FirstName">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="LastName">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="button" value="Submit">
</form>