如何在同一窗口中显示表单输出并隐藏表单

How to display form output in the same window and hide the form as well

本文关键字:表单 显示 输出 隐藏 窗口      更新时间:2023-09-26

从昨天开始,我一直在敲打它,但无济于事。我需要做的是制作表单并显示自定义贺卡。我们被允许使用图片作为卡片(将它们设置为输出的背景)。表单应允许用户输入一些自定义信息,例如收件人的姓名、颜色信息、字体大小和您认为合适的其他信息。应该有一个"制作卡片"按钮,当他们点击它时,隐藏表单(通过更改相应元素的 CSS 可见性或显示属性)并显示一张卡片,其设计简单而优雅,包含他们输入的文本和其他配置详细信息。

此外,我还需要根据他们在贺卡类型中的选择提供背景图片,并在贺卡上为他们选择的角色提供一张小贴纸大小的图片。

任何帮助将不胜感激。

以下是我的Java脚本CSS和HTML代码:

< script >
  function selectAll() {
    for (i = 0; i < document.forms[0]['charac'].length - 1; i++)
      document.forms[0]['charac'][i].checked = document.forms[0]['charac'][5].checked;
  }
document.forms[0]['charac'][5].addEventListener('click', selectAll);
function myCard() {
  document.getElementById("formOutput").style.backgroundImage = "url('birthday-card.jpg')";
  var selected;
  if (selected == document.forms[0]['card'][0]) {
    document.body.style.backgroundImage = "url('birthday-card.jpg')";
    document.getElementById("formOutput").innerHTML = "name";
  }
}
function myFunction() {
  var a = document.getElementById("txtName").value;
  var b = document.getElementById("txtName1").value;
  var c = document.getElementById("content-field").value;
  document.getElementById("formOutput").innerHTML = a + " " + b + " " + c;
} < /script>
<style> #wrapper {
  border: 3px black;
}
body {
  background-image: url("blank-card1.jpg");
  background-size: cover;
  background-repeat: no-repeat;
  text-align: center;
}
table {
  /*             background-image: image("blank-card.jpg");*/
  text-align: center;
  margin-left: auto;
  margin-right: auto;
}
table td {
  height: 15px;
  /*            width: 15px;*/
  text-align: left;
}
</style>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Generate a Greeting Card</title>
</head>
<body>
  <h3>Fill out the following information and select a card to view how it looks.</h3>
  <form id="formCard" action="">
    <fieldset>
      <legend>Personal information:</legend>
      <table>
        <tr>
          <td>
            <label for="txtName">Your Name<span style="color:red">*</span>
            </label>
          </td>
          <td>
            <input type="text" id="txtName" name="name" value=" " required/>
          </td>
        </tr>
        <tr>
          <td>
            <label for="txtName1">Your Family Member's
              <br>OR Friend's Name<span style="color:red">*</span>
            </label>
          </td>
          <td>
            <input type="text" id="txtName1" name="name1" value=" " required/>
          </td>
        </tr>
        <tr>
          <td>
            <label for="txtAddress">Address</label>
          </td>
          <td>
            <input type="text" id="txtAddress" name="address" value=" ">
          </td>
        </tr>
        <tr>
          <td>
            <label for="postalCode">Postal Code<span style="color:red">*</span>
            </label>
          </td>
          <td>
            <input type="text" id="postalCode" name="postalcode" value=" " required/>
          </td>
        </tr>
        <tr>
          <td>Province<span style="color:red">*</span>
          </td>
          <td>
            <select id="selProvince" name="province" required>
              <option value="" selected>Please select...</option>
              <option value="AB">Alberta</option>
              <option value="BC">British Columbia</option>
              <option value="MB">Manitoba</option>
              <option value="NB">New Brunswick</option>
              <option value="NFL">Newfoundland and Labrador</option>
              <option value="NW">Northwest Territories</option>
              <option value="NS">Nova Scotia</option>
              <option value="NU">Nunavut</option>
              <option value="ON">Ontario</option>
              <option value="PEI">Prince Edward Island</option>
              <option value="QC">Quebec</option>
              <option value="SK">Saskatchewan</option>
              <option value="YK">Yukon</option>
            </select>
          </td>
        </tr>
        <tr>
          <td>
            <label for="txtPhone">Phone<span style="color:red">*</span>
            </label>
          </td>
          <td>
            <input type="text" id="txtPhone" name="phone" value=" " required/>
          </td>
        </tr>
        <tr>
          <td>
            <label for="txtEmail">Email<span style="color:red">*</span>
            </label>
          </td>
          <td>
            <input type="text" id="txtEmail" name="email" required/>
          </td>
        </tr>
        <tr>
          <td>Would you like to send it
            <br>via email or mail<span style="color:red" required>*</span>
          </td>
          <td>
            <label>
              <input type="radio" name="route" value="m" />Mail</label>
            <label>
              <input type="radio" name="route" value="e" />Email</label>
          </td>
        </tr>
        <tr>
          <td>Type of Greeting Card:<span style="color:red">*</span>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
          </td>
          <td>
            <br>
            <label>
              <input type="radio" name="card" id="birthday" value="bd" />Birthday</label>
            <br>
            <label>
              <input type="radio" name="card" id="valentine" value="vd" />Valentines Day</label>
            <br>
            <label>
              <input type="radio" name="card" id="friendship" value="frd" />Friendship Day</label>
            <br>
            <label>
              <input type="radio" name="card" id="mother" value="md" />Mother's Day</label>
            <br>
            <label>
              <input type="radio" name="card" id="father" value="fad" />Father's Day</label>
            <br>
            <label>
              <input type="radio" name="card" id="newYear" value="nyd" />New Year's Day</label>
            <br>
            <label>
              <input type="radio" name="card" id="christmas" value="cd" />Christmas Day</label>
            <br>
            <br>
          </td>
        </tr>
        <tr>
          <td>Choose the person's
            <br>Favourite cartoon charcters:<span style="color:red">*</span>
            <br>
            <br>
            <br>
            <br>
            <br>
          </td>
          <td>
            <label>
              <input type="checkbox" name="charac" value="mickeyMouse" checked/>Mickey Mouse &amp; Clubhouse</label>
            <br>
            <label>
              <input type="checkbox" name="charac" value="scoobyDoo" />Scooby Doo</label>
            <br>
            <label>
              <input type="checkbox" name="charac" value="tomJerry" />Tom &amp; Jerry</label>
            <br>
            <label>
              <input type="checkbox" name="charac" value="tweetiePie" />Tweetie Pie</label>
            <br>
            <label>
              <input type="checkbox" name="charac" value="donaldDuck" />Donald Duck</label>
            <br>
            <label>
              <input type="checkbox" name="charac" value="selectAll" />Select All</label>
          </td>
        </tr>
        <tr>
          <td>Choose a colour for the
            <br>text on the card:</td>
          <!--color code from w3schools.org-->
          <td>
            <input type="color" id="html5colorpicker" onchange="clickColor(0, -1, -1, 5)" value="#ff0000" style="width:85%;">
          </td>
        </tr>
        <tr>
          <td>
            <label>Content</label>
          </td>
          <td>
            <textarea id="content-field" cols="20" rows="2" value=" "></textarea>
          </td>
        </tr>
        <tr>
          <td>Choose an image to put on
            <br>the card to make it special.</td>
          <td>
            <label>
              <input type="file" name="pic" accept="image/*">
            </label>
          </td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>
            <br>
            <br>
            <input onclick="myFucntion()" type="submit" value="SEND" />&nbsp;
            <input type="reset" value="CLEAR" />
          </td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </fieldset>
  </form>
  <p><span id="formOutput"></span>
  </p>
</body>
</html>

你能

做这样的事情吗:

<form ...>
    <fieldset id='fields'>
        ...
        <input onclick="showOutput()" type="submit" value="SEND" />
    </fieldset>
    <div id='formOutput' style='display: none;'>
    </div>
</form>
function showOutput() {
    /* Fill formOutput */
    ....
    document.getElementById('fields').style.display = 'none';
    document.getElementById('formOutput).style.display = '';
}

但是由于 SEND 按钮是一个提交按钮,我不知道您是否要在 Javascript 代码中进行所有处理。

此外,如果要允许在两个视图之间切换,"切换"按钮必须位于字段集之外(或者需要在 formOutput 内部使用按钮才能返回到输入视图)。