admin管理员组

文章数量:1325137

I'm a beginner about javascript, and I have some doubts about the variables of this language.

For example, I have this code:

<html>
<head>
<title>Prova_Index</title>

<script language="javascript" type="text/javascript">
function stampa(){
var Name = document.name.utente.value;
document.Write(Name);
}
</script>

</head>

<body>
<form name="name">
<p> Write some text:</p>
<input type="text" id="utente">

</form>
<input type="submit" value="Click me" onClick="stampa()"> 
</body>
</html>

This is the code, my doubts is specialy how save the values of the textbox with the id 'utente'.

I'm a beginner about javascript, and I have some doubts about the variables of this language.

For example, I have this code:

<html>
<head>
<title>Prova_Index</title>

<script language="javascript" type="text/javascript">
function stampa(){
var Name = document.name.utente.value;
document.Write(Name);
}
</script>

</head>

<body>
<form name="name">
<p> Write some text:</p>
<input type="text" id="utente">

</form>
<input type="submit" value="Click me" onClick="stampa()"> 
</body>
</html>

This is the code, my doubts is specialy how save the values of the textbox with the id 'utente'.

Share Improve this question edited Jun 15, 2012 at 13:19 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Jun 15, 2012 at 13:14 Gabriel ButoeruGabriel Butoeru 1311 gold badge3 silver badges14 bronze badges 2
  • 4 Please do not put "help", "urgent" or similar phrases in the question title. Please edit your question to a) use a meaningful title, b) explain exactly what you are trying to do. Right now it's not really clear what you want. How do you want to "save" the value? – ThiefMaster Commented Jun 15, 2012 at 13:14
  • Learn more about the DOM: getElementById. – Felix Kling Commented Jun 15, 2012 at 13:19
Add a ment  | 

4 Answers 4

Reset to default 3

write this code by replacing your code

function stampa()
            {
                var Name = document.getElementById('utente').value;
                alert(Name);
                return false;
            }
    <input type="text" id="utente">

    <input type="submit" value="Click me" onclick="return stampa();" /> 

Instead of var Name = document.name.utente.value;, you probably want to use something like var Name = document.getElementById('utente').value;

Really, you might want to consider using jQuery instead. It's far more reliable for cross-browser oddities than using plain old DOM manipulation.

This Javascript should store the value of that input element into the variable 'Name' and then write it out.

var Name = document.getElementById("utente").value;

document.write(Name);

I will forewarn you that when you use HTML <form> tags and try to execute a function using a submit input, the page will submit and then the onClick function will not be executed.

First of all. Please explain us what you want to do. It isn't clear to us.

Second of all. I remend using ID's when you are beginning with JavaScript programming.

When you take a look at your code. It will submit after the function is ready. So you can try and use this:

<input type="submit" value="click me" onclick="stampa( ); return false;" />

It won't submit the form and you can see your result. You can also use an alert( ); function for debugging or console.log( ); if you use Firefox with Firebug or Opera with Dragonfly.

You can try this in your script:

console.log( document.getElementById( 'something' ).value( ) );

本文标签: javascriptoutput a variable onclickStack Overflow