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


displaying result of HTML form on with js function

我知道你不能在html中真正拥有变量,但我想知道这是否可能。我四处搜索,找不到任何清楚地回答我的问题的东西。希望有人听到可以指出我正确的方向。以下是我的想法:

<!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>

这会起作用还是类似的东西?谢谢,山姆

如果您的意思是将输入的内容用作变量:

<!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>

我从您的问题中了解到的是您想访问<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>