简体   繁体   English

用js函数显示HTML表单的结果

[英]displaying result of HTML form on with js function

I know that you cannot really have variables in html, but I'm wondering if this is possible. 我知道您实际上不能在html中有变量,但是我想知道是否可行。 I have searched around and cannot find anything that clearly answers my question. 我四处搜寻,找不到任何能清楚回答我问题的内容。 Hopefully someone hear can point me in the right direction. 希望有人听到可以指出我正确的方向。 Here's what I have in mind: 这就是我的想法:

<!DOCTYPE html>
<html>
<body>

<input type="text" name="test">
<input type="submit" onclick="myFunction(test)">
<script type="text/javascript">
function myFunction(test)
{
alert("Welcome " + test);
}
</script>

</body>
</html>

Would this work or something like it? 这项工作或类似的工作吗? Thanks, Sam 谢谢山姆

If you mean using the contents of an input as a variable: 如果要使用输入内容作为变量:

<!DOCTYPE html>
<html>
<body>

<input type="text" id="some_id" name="test">
<input type="submit" onclick="myFunction(document.getElementById('some_id').value)">
<script type="text/javascript">
function myFunction(test)
{
alert("Welcome " + test);
}
</script>

</body>
</html>

What I understood from your question is you want to access <input> and send it to function(test) try this: 我从您的问题中了解到,您想访问<input>并将其发送给function(test)尝试以下操作:

<input type="text" name="test" id="test"> <-- Give ID here
<input type="submit" onclick="myFunction(test)"> <--Send same ID here
<script type="text/javascript">
function myFunction(test)
{
alert("Welcome " + test.value);
}
</script>

</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM