繁体   English   中英

如何使用JavaScript在新窗口的html字段中显示值?

[英]How do I display value in a html field in a new window with javascript?

我基本上是在尝试验证输入字段,打开新窗口并在新窗口中显示输入字段的内容。 我可以打开新窗口,但不会将文本发布到字段“ displayText”。

的HTML:

<head>
    <title>Q3</title>
    <script type ="text/javascript" src="functions.js"></script>
</head>
<body>
  <!-- Form -->
  <form name="f3">
    Text: <input type="text" id="tb5" name="tb5"><br/>
    <button onclick="validateQ3()" Type="button" id="b1" name="b1">Submit</button>
  </form>

javascript:

function validateQ3() {
    // Get value from text box
    var a = document.getElementById("tb5").value;
        // Check that textbox is neither empty nor contains null
    if (a == null || a == "")
    {
        // if error, display error
        alert("Textbox cannot be empty");
        return false;
    }
    // Check text field >= 5
    if (a.length < 5)
    {
        alert("Field must be longer than 5 characters");
        return false;
    }
    else 
    {
        // Call newWindow() function
        newWindow(a);
    }
}//validateQ3()

function newWindow(a) {
    // Open new window
    var newWindow = window.open("", "mypopup", "height=300 , width=300");
    newWindow.document.write("<p id='displayText'></p>");
    if (window.focus)
    {
        newWindow.focus();
    }
    var tmp = newWindow.document;
    tmp.write('<html><head><title>mypopup</title></head>');
    tmp.write('<body><p id="displayText"></p></body></html>');
    document.getElementById("displayText").innerHTML = a;
    tmp.close();
}

任何帮助表示赞赏

您正在尝试在当前窗口而不是新窗口上设置元素的innerHTML,在第一个窗口上不存在displayText,将文档更改为:

 tmp.getElementById("displayText").innerHTML = a;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM