Javascript警报未正确显示
是否有人知道如何解决以下问题:我希望用户输入的表单在另一个HTML文件中发出警报 这就是我到目前为止写的。它适用于名为“name”的id,但我不知道为什么不适用于“position” “index.html”文件:Javascript警报未正确显示,javascript,alerts,Javascript,Alerts,是否有人知道如何解决以下问题:我希望用户输入的表单在另一个HTML文件中发出警报 这就是我到目前为止写的。它适用于名为“name”的id,但我不知道为什么不适用于“position” “index.html”文件: 函数myFunction(){ name=document.getElementById(“name”).value; 位置=document.getElementById(“位置”).value; window.location.replace(“signature.html”)
函数myFunction(){
name=document.getElementById(“name”).value;
位置=document.getElementById(“位置”).value;
window.location.replace(“signature.html”);
返回false;
}
名称:
职位:
“signature.html”文件-请注意警报(名称)和警报(位置)=>警报(名称)有效,但警报(位置)无效。我想知道如何解决这个问题
<html>
<head>
</head>
<body>
<div class="signature_general">
<div class = "signature_middle">
<div id = "signature_details">
<font size="2px">Regards,</font><br>
<b><font color="#808080" size="3px" id="nameInput">Francisco Jurado</font></b><br>
<font size="2px" id="posInput">Accounting Systems Team Leader</font>
</div>
</div>
</div>
<script>
alert(name);
alert(position);
</script>
</body>
</html>
问候,
弗朗西斯科·朱拉多
会计系统小组组长
警报(名称);
警戒(位置);
非常感谢
编辑:我注意到我得到了一个错误,“位置”是“未定义的”。有人知道吗?您在
index.html
中设置的名称与您在signature.html
警报中打印的名称不同
您可以通过在两个位置将name
重命名为name2
来看到这一点
如果要将这些变量从一个页面传递到另一个页面,我建议使用查询字符串,您可以将html文件更改为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form class="formulario" action="signature.html" method="get">
Name: <input type="text" name="name" id="name"><br>
Position: <input type="text" name="position" id="position"><br>
<br>
<input type="submit" value="Generate Signature">
</form>
</body>
</html>
文件
名称:
职位:
您的signature.html文件为:
<html>
<head>
</head>
<body>
<div class="signature_general">
<div class = "signature_middle">
<div id = "signature_details">
<font size="2px">Regards,</font><br>
<b><font color="#808080" size="3px" id="nameInput">Francisco Jurado</font></b><br>
<font size="2px" id="posInput">Accounting Systems Team Leader</font>
</div>
</div>
</div>
<script>
var values = window.location.search.substring(1).split('&')
var name = values[0].split('=')[1]
var position = values[1].split('=')[1]
alert(name)
alert(position)
</script>
</body>
</html>
问候,
弗朗西斯科·朱拉多
会计系统小组组长
var values=window.location.search.substring(1.split('&'))
变量名称=值[0]。拆分('=')[1]
变量位置=值[1]。拆分('=')[1]
警报(名称)
警报(位置)
显然,您没有为“name”和“position”标记变量。试试这个:var name='test1',position='test2';警报(名称);警戒(位置)@Sphinx编辑:你的意思是在“signature.html”文件中吗?@Sphinx我尝试了以下内容(可能不是你的意思):在“index.html”文件中声明,如下所示:var name=document.getElementById(“name”).value;var position=document.getElementById(“position”).value;达思网。我注意到,如果我用空格输入一个值,它将显示“hello+world”,而不是显示“hello+world”。如何修复此问题?还有@,显示为“%40”。谢谢。编辑:通过此链接解决:
<html>
<head>
</head>
<body>
<div class="signature_general">
<div class = "signature_middle">
<div id = "signature_details">
<font size="2px">Regards,</font><br>
<b><font color="#808080" size="3px" id="nameInput">Francisco Jurado</font></b><br>
<font size="2px" id="posInput">Accounting Systems Team Leader</font>
</div>
</div>
</div>
<script>
var values = window.location.search.substring(1).split('&')
var name = values[0].split('=')[1]
var position = values[1].split('=')[1]
alert(name)
alert(position)
</script>
</body>
</html>