Javascript警报无法正确显示

时间:2018-01-17 18:58:05

标签: javascript alerts

有谁知道如何解决以下问题:我希望用户输入表单,以便在其他HTML文件中发出警报。

这是我到目前为止所写的内容。它适用于名为'name'的id,但我不知道为什么它不适用于'position'。

“index.html”文件:

<script>
    function myFunction() {
        name = document.getElementById("name").value;
        position = document.getElementById("position").value;

        window.location.replace("signature.html");
        return false;
    }
</script>

<form class="formulario" onsubmit="return myFunction()">
      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> 

“signature.html”文件 - 注意警报(名称)和警报(位置)=&gt;警报(名称)有效,但不是警报(位置)。我想了解如何解决这个问题。

<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>

非常感谢

编辑:我注意到我收到错误,其中“位置”是“未定义”。有谁知道?

2 个答案:

答案 0 :(得分:0)

您在index.html中设置的名称与您在signature.html的提醒中打印的名称不同。

您可以通过在两个地方将name重命名为name2来查看此内容。

如果您想将这些变量从一个页面传递到另一个页面,我建议使用查询字符串how to exchange variables between two HTML pages?

答案 1 :(得分:0)

您可以将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>