【问题标题】:html form that passes information to a variable in javascript [closed]将信息传递给javascript中的变量的html表单[关闭]
【发布时间】:2014-07-31 05:52:31
【问题描述】:

我是 Javascript 新手,我正在尝试向我的 html 页面添加一个表单,将输入信息发送到一个变量,以便我以后可以使用该变量。它可以替代输入提示,从而节省变量的信息。有谁知道如何做到这一点?

【问题讨论】:

  • HTML 不会将数据“发送”到 JavaScript。但是 JavaScript 可以检查 HTML 元素并从中获取数据。你有没有做过这方面的尝试?许多介绍性的 JavaScript 教程将展示如何获取 HTML 元素并从中提取一些信息。

标签: javascript html forms variables


【解决方案1】:

总结

我假设您使用的是标准输入控件,让我们假设文本...首先您必须将表单放入 HTML 中,因此在我们的 JavaScript 中,我们可以使用其 id 找到输入。我们还需要一个按钮,一个常规按钮,而不是提交按钮,供用户在决定输入时点击。我们将附加一个点击事件,以便当我们的用户点击我们的 JavaScript 开始工作所以这是我们的 HTML 的开始:

<input type="text" id="input">
<button onclick="setClick()">
    Submit
</button>

在您说“是的!我知道!”之前,让我们看看 JavaScript 方面...我们需要首先获取文本框的值,以便在单击它时获取文本框的 value 属性并插入到变量input.. 所以这里是JavaScript:

function setClick()
{
    var input = document.getElementById('input').value; 
    //saves the current value of the textbox into the input variable
    alert(input);
}
/*
    So now each time the button is clicked the variable "input" is assigened
    the textbox's value
*/

完整代码

<!DOCTYPE html>
<html>
<head>
<title>
Submit
</title>
<script type="text/javascript">
function setClick()
{
    var input = document.getElementById('input').value; 
    //saves the current value of the textbox into the input variable
    alert(input);
}
    /*
    So now each time the button is clicked the variable "input" is assigened
    the textbox's value
    */
</script>
<body>
<input type="text" id="input">
<button id="submit" onclick="setClick()">
    Submit
</button>
</body>
</html>

【讨论】:

  • 这正是我的意思!谢谢你的回答!
  • 没有问题 ;)。当我还在学习 JavaScript 时,我的感觉就像你一样,谷歌搜索不起作用;我不知道要搜索什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多