我是 Javascript 的新手,正在努力让一个简单的表格起作用。我能得到一些建议吗?
I am brand new to Javascript, and am struggling to get a simple form to work. Can I please get some advice?
我的代码如下。正在尝试为作业创建一个简单的表格。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Adoption Form</title>
<script>
function getInfo(){
var first =
document.forms["formInfo"]["first"].value;
var last =
document.forms["formInfo"]["last"].value;
var applicantInfo = "My first name is " + first + " My last name is " + last ".";
document.getElementById("info").innerHTML = applicantInfo;
}
</script>
</head>
<body>
<div>
<form id="formInfo" name="formInfo">
<p>My first name is <input name="first" type="text" id="first" title="first"></p>
<p> My last name is <input name="last" type="text" id="last" title="last"></p>
<p><input type="button" value="Send Information" onClick="getInfo()"></p>
</form>
</div>
<div id="info">
</div>
</body>
</html>
每当我在浏览器中按下按钮时,都没有任何反应。我哪里错了?
首先有两个错误是onclick
和小c
不是onClick
。
其次,您在 last
之后缺少 +
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Adoption Form</title>
<script>
function getInfo() {
var first = document.forms["formInfo"]["first"].value;
var last = document.forms["formInfo"]["last"].value;
var applicantInfo =
"My first name is " + first + " My last name is " + last + ".";
document.getElementById("info").innerHTML = applicantInfo;
}
</script>
</head>
<body>
<div>
<form id="formInfo" name="formInfo">
<p>
My first name is
<input name="first" type="text" id="first" title="first" />
</p>
<p>
My last name is
<input name="last" type="text" id="last" title="last" />
</p>
<p>
<input type="button" value="Send Information" onclick="getInfo();" />
</p>
</form>
</div>
<div id="info"></div>
</body>
</html>
此代码有效,这里是 JSFIDDLE
您只是在变量赋值的末尾缺少连接运算符 (+)。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Adoption Form</title>
<script>
function getInfo(){
var first =
document.forms["formInfo"]["first"].value;
var last =
document.forms["formInfo"]["last"].value;
var applicantInfo = "My first name is " + first + " My last name is " + last + ".";
document.getElementById("info").innerHTML = applicantInfo;
};
</script>
</head>
<body>
<div>
<form id="formInfo" name="formInfo">
<p>My first name is <input name="first" type="text" id="first" title="first"></p>
<p> My last name is <input name="last" type="text" id="last" title="last"></p>
<p><input type="button" value="Send Information" onClick="getInfo()"></p>
</form>
</div>
<div id="info">
</div>
</body>
</html>
我的代码如下。正在尝试为作业创建一个简单的表格。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Adoption Form</title>
<script>
function getInfo(){
var first =
document.forms["formInfo"]["first"].value;
var last =
document.forms["formInfo"]["last"].value;
var applicantInfo = "My first name is " + first + " My last name is " + last ".";
document.getElementById("info").innerHTML = applicantInfo;
}
</script>
</head>
<body>
<div>
<form id="formInfo" name="formInfo">
<p>My first name is <input name="first" type="text" id="first" title="first"></p>
<p> My last name is <input name="last" type="text" id="last" title="last"></p>
<p><input type="button" value="Send Information" onClick="getInfo()"></p>
</form>
</div>
<div id="info">
</div>
</body>
</html>
每当我在浏览器中按下按钮时,都没有任何反应。我哪里错了?
首先有两个错误是onclick
和小c
不是onClick
。
其次,您在 last
+
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Adoption Form</title>
<script>
function getInfo() {
var first = document.forms["formInfo"]["first"].value;
var last = document.forms["formInfo"]["last"].value;
var applicantInfo =
"My first name is " + first + " My last name is " + last + ".";
document.getElementById("info").innerHTML = applicantInfo;
}
</script>
</head>
<body>
<div>
<form id="formInfo" name="formInfo">
<p>
My first name is
<input name="first" type="text" id="first" title="first" />
</p>
<p>
My last name is
<input name="last" type="text" id="last" title="last" />
</p>
<p>
<input type="button" value="Send Information" onclick="getInfo();" />
</p>
</form>
</div>
<div id="info"></div>
</body>
</html>
此代码有效,这里是 JSFIDDLE
您只是在变量赋值的末尾缺少连接运算符 (+)。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Adoption Form</title>
<script>
function getInfo(){
var first =
document.forms["formInfo"]["first"].value;
var last =
document.forms["formInfo"]["last"].value;
var applicantInfo = "My first name is " + first + " My last name is " + last + ".";
document.getElementById("info").innerHTML = applicantInfo;
};
</script>
</head>
<body>
<div>
<form id="formInfo" name="formInfo">
<p>My first name is <input name="first" type="text" id="first" title="first"></p>
<p> My last name is <input name="last" type="text" id="last" title="last"></p>
<p><input type="button" value="Send Information" onClick="getInfo()"></p>
</form>
</div>
<div id="info">
</div>
</body>
</html>