使用JavaScript将按钮值添加到另一页上的文本中

时间:2018-07-01 21:51:20

标签: javascript html arrays button text

我现在有一段时间要尝试一些东西,好像卡住了...

我在第1页上有一个按钮,在第2页上有结果 想法是将值中的特定文本放置在page2上的文本中的某个位置,或至少放置在文本末尾的某个位置。

就目前而言,按钮的值和名称正进入第二页,但是我在如何使其更接近“ this is”或“ this”和“ is”之间缺少一些信息

非常感谢您的建议。

谢谢!

page1.html

<!doctype html>
<html lang="en">
   <head>
      <title>Document</title>
   </head>
   <body>
     <article>
        <h2>Button hidden element to a text on next page</h2>
           <form action="page2.html" autocomplete="on">
           <fieldset class="btn">
              <input type="submit" id="1" value="button 1" name="one" />
           </fieldset>
           </form>
    </article>
    <script src="script2.js"></script>
   </body>
</html>

page2.html

<!doctype html>
<html lang="en">
   <head>
      <title>Document</title>
   </head>
   <body>
      <article>
         <div class="results">
            <h3>You entered the following data:</h3>
            <p>this is</p>
         </div>
      </article>
      <script src="script3.js"></script>
   </body>
</html>

script2.js

"use strict";
var queryArray = []; 
function populateInfo() {
   if (location.search) {
    var queryData = location.search;
    var hiddenInputs = document.querySelectorAll("input[type=hidden]");
    queryData = queryData.substring(1, queryData.length);
    queryArray = queryData.split("&");
    }
    for (var i=0; i < queryArray.length; i++)
    {
        hiddenInputs[i].value = 
 queryArray[i].substring(queryArray[i].lastIndexOf("=") + 1);
    }
  }
if (window.addEventListener) {
   window.addEventListener("load", populateInfo, false);
 } else if (window.attachEvent) {
  window.attachEvent("onload", populateInfo);
}

script3.js

"use strict";
   function parseData() {
     var formData = decodeURIComponent(location.search);
     var formArray = [];
     var list = document.querySelector("div.results p");
     formData = formData.substring(1, formData.length);
     while (formData.indexOf("+") !== -1) {
       formData = formData.replace("+", " ", " ");
   {
       formData = decodeURIComponent (formData);
       formArray = formData.split("&");
       for (var i = 0; i < formArray.length; i++)
      {
            var newItem = document.createElement("p");
            newItem.innerHTML = formArray[i];
            list.appendChild(newItem);
        }
    }
 }
 }
    if (window.addEventListener)
 {
    window.addEventListener("load", parseData, false);
}   else if (window.attachEvent)
{
    window.attachEvent("onload", parseData);
 }

0 个答案:

没有答案