表单提交后,我需要显示一条包含变量(来自表单的答案)的消息

Posted

技术标签:

【中文标题】表单提交后,我需要显示一条包含变量(来自表单的答案)的消息【英文标题】:I need to display a message containing variables (answers from form) after form submit 【发布时间】:2014-12-11 03:17:57 【问题描述】:

我需要在故事中显示表单中的答案。只有在表单成功提交(不是警报)后,故事才必须显示在表单下方,并且表单保留在页面上(不是新页面)。我可以将表单值插入故事中,但在提交表单后需要帮助来显示故事。除了 htmljavascript,我不能使用任何东西。我认为这可以通过innerHTML 来完成。

<head><title>Questions</title>
<script type = "text/javascript" src="quiz1.js">
</script>
</head><body>
<h1>Tell Me About Yourself</h1>
<form name = "the_form" action = "" method = "post"
onSubmit = "var the_result = checkMandatory(); return the_result;">
Full Name:<input type = "text" name = "name" id = "name" /><br/>
Favourite Animal:<input type = "text" name = "animal" id = "animal"><br/>
Favourite Food:<input type = "text" name = "favFood" id = "favFood"><br/>
Favourite Destination:<input type = "text" name = "destination" id = "desitnation"><br/>
Least Favourite Food:<input type = "text" name = "leastFav" id = "leastFav"><br/>
Happiest Moment:<input type = "text" name = "moment" id = "moment"><br/>
Adjective that describes you:<input type = "text" name = "adjective" id = "adjective"><br/>
<br>
<input type="button" value="Submit" onClick = "checkMandatory(); return false;"/><br />
<br />
</form>

<div id="storyDiv"></div>


</body>
</html>


function checkMandatory()

// check the text field
// make a var for each question to access easier eg "favMeal"
var name = window.document.the_form.name.value;
//! means 'not'... flips around the if 
if (!(name.indexOf(" ") > 0))

    alert("You must give your full name.");
 //return false stops the program
    return false;
 else 
//firstName checks all character from 0 to whenever (space) occurs and strips it
        var firstName = name.substring(0,name.indexOf(" "));
        var name = window.document.the_form.name.value;
        var animal = window.document.the_form.animal.value;
        var favFood = window.document.the_form.favFood.value;
        var destination = window.document.the_form.destination.value;
        var leastFav = window.document.the_form.leastFav.value;
        var moment = window.document.the_form.moment.value;
        var adjective = window.document.the_form.adjective.value;

//alert("first name is " + firstName);
//use alert firstName to test the firstName function
document.write("The young person's name was "+firstName+". "+firstName+" loved to ride
 "+animal+
" almost every day. "+firstName+"'s second happiest moment, only next to "+moment+", was in 
"+destination+", where "+favFood+
" was served for breakfast, lunch and dinner. It was only when "+firstName+" was told that   
"+favFood+
" is actually made from "+animal+", that it instantly became "+firstName+"'s least    
favourite food, even worse than "+leastFav+
", and that made "+firstName+" feel very "+adjective+" indeed.")
//document.getElementById('storyDiv').innerHTML = document.getElementById('name').value;



//document.getElementById(‘storyDiv’).innerHTML="The boy's name was "+firstName;
//document.write(‘storyDiv’).innerHTML="The boy's name was " + firstName;



【问题讨论】:

【参考方案1】:

您可以通过使用 ajax 发布表单来实现此目的。

不要调用 writethetext();在您的提交按钮中

我将在我的解决方案中使用 jQuery:

$(function() 
  $("form").on("submit", function(e) 
    var data = JSON.stringify($("form").serializeArray());
    e.preventDefault();
    $.post("yourserver/path/", data, function(result) 
      writethetext(result);

    );
  );
);

function checkMandatory() 
  // check the text field
  // make a var for each question to access easier eg "favMeal"
  var name = window.document.the_form.name.value;
  //! means 'not'... flips around the if 
  if (!(name.indexOf(" ") > 0)) 
    alert("You must give your full name.");
    //return false stops the program
    return false;
   else 
    //firstName checks all character from 0 to whenever (space) occurs and strips it
    var firstName = name.substring(0, name.indexOf(" "));
    //alert("first name is " + firstName);
    //use alert firstName to test the firstName function
  




function writethetext() 
  document.getElementById(‘storyDiv’).innerHTML =
    ("There once was a boy named" + name;)
  var firstName = name.substring(0, name.indexOf(" "));
  var name = window.document.the_form.name.value;
  var animal = window.document.the_form.animal.value;
  var favFood = window.document.the_form.favFood.value;
  var destination = window.document.the_form.destination.value;
  var leastFav = window.document.the_form.leastFav.value;
  var moment = window.document.the_form.moment.value;
  var adjective = window.document.the_form.adjective.value;



function writethetext(text) 
  document.getElementById(‘storyDiv’).innerHTML = text;

<html>

<head>
  <title>Questions</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="quiz1.js">
  </script>
</head>

<body>
  <h1>Tell Me About Yourself</h1>
  <form name="the_form" action="" method="post" onSubmit="var the_result = checkMandatory(); return the_result;">
    Full Name:
    <input type="text" name="name" id="name" />
    <br/>Favourite Animal:
    <input type="text" name="animal" id="animal">
    <br/>Favourite Food:
    <input type="text" name="favFood" id="favFood">
    <br/>Favourite Destination:
    <input type="text" name="destination" id="desitnation">
    <br/>Least Favourite Food:
    <input type="text" name="leastFav" id="leastFav">
    <br/>Happiest Moment:
    <input type="text" name="moment" id="moment">
    <br/>Adjective that describes you:
    <input type="text" name="adjective" id="adjective">
    <br/>
    <br>
    <input type="button" value="Submit" onClick="checkMandatory();  
return           false;" />
    <br />
    <br />
  </form>
  <div id="storyDiv"></div>
</body>

</html>

【讨论】:

以上是关于表单提交后,我需要显示一条包含变量(来自表单的答案)的消息的主要内容,如果未能解决你的问题,请参考以下文章

jQuery表单验证

在 laravel 中通过 ajax 提交简单的表单,之后,将新行插入到现有表中,其中包含来自提交表单的数据

jQuery在使用ajax验证后提交表单

PHP - 如何在提交后隐藏表单

表单提交前的引导模式

Vue:在另一个页面上显示来自表单提交的响应