0

I know that you cannot really have variables in html, but I'm wondering if this is possible. 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

4

2 回答 2

3

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>
于 2012-08-16T15:23:41.727 回答
0

我从你的问题中了解到的是你想访问<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>
于 2012-08-16T15:24:27.677 回答