gpt4 book ai didi

javascript - 我如何使用 javascript 从表单中获取信息?

转载 作者:行者123 更新时间:2023-11-30 09:02:57 25 4
gpt4 key购买 nike

我有一个用户输入一些数据的表单,可以是复选框、单选按钮、文本字段等

当用户点击提交按钮时,我想用输入的任何数据刷新页面

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
</head>
<body id="ref">
<form>
Please enter your name:<input type="text" id="name" />
Please enter your age:<input type="text" id="age" />
</form>
<input type="button" onclick="c()" value="submit">

<script type="text/javascript">

function c()
{
var o = document.getElementById('ref');
o.innerHTML = '';

var n = document.createElement('p');
var nam = document.getElementById('name');
n.innerHTML = "Your name is: " + nam;
o.appendChild(n);

var a = document.createElement('p');
var ag = document.getElementById('age');
a.innerHTML = "Your age is: " + ag;
o.appendChild(a);

//how do i get the info from the form? because both nam and ag are coming up null

}

</script>
</body>
</html>

我猜这不起作用是因为页面刷新然后尝试通过不再存在的 id 获取元素.. 正确的方法是什么?

最佳答案

您混淆了对象及其属性。在这里,您将获得“年龄”字段的 HTMLInputElement 实例:

var ag = document.getElementById('age');

但是在这里你使用那个对象就好像它是一个简单的值:

a.innerHTML = "Your age is: " + ag;

HTMLInputElement 对象有一个 value 字段,您可以使用它:

a.innerHTML = "Your age is: " + ag.value;

另外,这样做会完全破坏页面:

var o = document.getElementById('ref');
o.innerHTML = '';

...因为您已经为 body 元素指定了 ID“ref”。完全替换 body 元素会完全替换 body 元素,因此您不能依赖仅作为该元素的从属存在的对象。

通常的做法是填充一个元素,然后(可选)删除不再需要的元素。例如 ( live copy ):

HTML:

<form id="theForm">
Please enter your name:<input type="text" id="name" />
Please enter your age:<input type="text" id="age" />
<input type="button" onclick="c()" value="submit">
</form>
<div id="result">
</div>

(请注意,为方便起见,我将按钮移到了表单中。)

JavaScript:

function c() {
var form = document.getElementById("theForm"),
nameField = document.getElementById("name"),
ageField = document.getElementById("age"),
result = document.getElementById("result");

form.parentNode.removeChild(form);
result.innerHTML =
"Your name is " + nameField.value +
" and your age is " + ageField.value;
}

在那里,当按下按钮时,我删除了表单并填写了“result”div。

如果需要,您可以动态添加“结果”div (live copy):

HTML:

<form id="theForm">
Please enter your name:<input type="text" id="name" />
Please enter your age:<input type="text" id="age" />
<input type="button" onclick="c()" value="submit">
</form>

JavaScript:

function c() {
var form = document.getElementById("theForm"),
nameField = document.getElementById("name"),
ageField = document.getElementById("age"),
result;

result = document.createElement("div");
result.innerHTML =
"Your name is " + nameField.value +
" and your age is " + ageField.value;
form.parentNode.insertBefore(result, form);
form.parentNode.removeChild(form);
}

如果将 id 值更改为 name 值(live copy),则可以使用更简洁、更自然的语法访问字段:

HTML:

<form name="theForm">
Please enter your name:<input type="text" name="name" />
Please enter your age:<input type="text" name="age" />
<input type="button" onclick="c()" value="submit">
</form>

JavaScript:

function c() {
var form = document.theForm,
nameField = form.name,
ageField = form.age,
result;

result = document.createElement("div");
result.innerHTML =
"Your name is " + nameField.value +
" and your age is " + ageField.value;
form.parentNode.insertBefore(result, form);
form.parentNode.removeChild(form);
}

进一步阅读:

关于javascript - 我如何使用 javascript 从表单中获取信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7693196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com