编写一个javascript函数,只需在单击按钮时将表单的详细信息打印到另一个html页面

Writing a javascript function that just prints details of a form to another html page when a button is clicked.

本文关键字:表单 按钮 详细信息 打印 页面 html 另一个 单击 一个 javascript 函数      更新时间:2023-09-26

所以我试图从表单中获取信息,一个是用户可以更改的数字,另一个是常量浮点数字。当用户输入一个数字时,他们会按提交,也就是我希望输出打印到我的确认网页上的时候。我在网上没有看到太多关于这方面的内容,所以如果有人能给我一些建议,或者只是为我提供一些在线材料,我将不胜感激。还应该提到的是,我不允许使用任何javascript库来做这件事,以防万一。感谢

啊,我感到很同情。我认为你是在同一个页面上做这件事的(如果不是,你可能在错误的地方&我想你想要PHP)。

如果是这样的话,这里有一些非常粗糙的东西让你开始,并附上一些评论。

请注意,关于JS和DOM有大量的信息,因为它本质上是该语言的主要目的。这表明你看起来并不是很认真,但我知道一开始很难知道你在找什么。MDN通常在细节方面非常出色——看看这里使用的方法,它们都会有比我更好的例子。

// Listen for a 'click' on the element with an ID of 'submit',
// and run the 'printSomeCrap' function:
document.querySelector('#submit').addEventListener('click', printSomeCrap, false)
function printSomeCrap() {
  // Grab the constant from the element with the id 'constant'
  var constant = document.querySelector('#constant').textContent
  // Grab whatever was typed in from the element with the id 'input'
  var input = document.querySelector('#input').value
  // Then find the element with the ID of 'output', and print out
  // what was just grabbed as a string:
  document.querySelector('#output').innerHTML = `Constant: ${constant}, Input: ${input}`
}
<input id="input" type="text" placeholder="Just type something">
<p id="constant">0.123456789</p>
<button id="submit">Then press this button</button>
<div id="output"></div>