i'm studying javascript but i can't find some clear reference about how getting and treat data out of the HTML forms.
Here an example:
THE FORM:
<HTML>
<HEAD>
<TITLE>Database Lookup</TITLE>
<script src="basic.js"></script></HEAD>
<BODY>
<H1>Database Lookup</H1>
<FORM action="javascript: submitForm();">
Please enter the ID of the publisher you want to find: <BR>
<INPUT TYPE="TEXT" NAME="id">
<BR>
<INPUT TYPE="SUBMIT" value="Submit" > </FORM>
</BODY>
<HTML>
//HERE JAVASCRIPT Javascript BASIC.js:
function submitForm()
{
var idsearched=document.getElementById("id").innerHTML;
document.write("idsearched");
}
I would like to know what i'm doing wrong, because nothing happen when i click submit. And which is the better solution for handling forms with javascript?? Using "action"? or which of other attributes?
i'm studying javascript but i can't find some clear reference about how getting and treat data out of the HTML forms.
Here an example:
THE FORM:
<HTML>
<HEAD>
<TITLE>Database Lookup</TITLE>
<script src="basic.js"></script></HEAD>
<BODY>
<H1>Database Lookup</H1>
<FORM action="javascript: submitForm();">
Please enter the ID of the publisher you want to find: <BR>
<INPUT TYPE="TEXT" NAME="id">
<BR>
<INPUT TYPE="SUBMIT" value="Submit" > </FORM>
</BODY>
<HTML>
//HERE JAVASCRIPT Javascript BASIC.js:
function submitForm()
{
var idsearched=document.getElementById("id").innerHTML;
document.write("idsearched");
}
I would like to know what i'm doing wrong, because nothing happen when i click submit. And which is the better solution for handling forms with javascript?? Using "action"? or which of other attributes?
Share Improve this question asked Sep 15, 2013 at 17:33 ciaobenciaoben 511 gold badge2 silver badges7 bronze badges 5- var idsearched=document.getElementById("id").value;document.write(idsearched); – Voonic Commented Sep 15, 2013 at 17:35
- w3schools./tags/att_form_action.asp – u_mulder Commented Sep 15, 2013 at 17:36
-
I don't see any element with
id="id"
, justNAME="id"
. – Sergio Commented Sep 15, 2013 at 17:38 -
You say that nothing happens when you click submit, but what do you expect to happen? Specifically, if you do
document.write
from within a .js file, where do you think its output will end up? Also, the second<html>
tag needs to be an end tag. – Mr Lister Commented Sep 15, 2013 at 17:51 - ty to all. Sergio you were right, stupid stupid mistake... it make me think that the error was at the base of my little knowledge... searching the internet i find a lot of different ways for handling form with js so i wanted to know why mine didn't work. @MrLister i didn't notice the end tag.. about the "document.write" i expected to overwrite all the content of the page with my form's input. – ciaoben Commented Sep 15, 2013 at 18:01
3 Answers
Reset to default 1The value of form elements are contained in their value attribute. Try the modified code snippet below.
Note: ("idsearched")
should be without quote because it is a variable and not a string.
var idsearched=document.getElementById("id").value;
document.write(idsearched);
You must add an id attribute to the form element.
<INPUT TYPE="TEXT" NAME="id" id="id">
Use this line to manually submit your form
<INPUT TYPE="button" value="Submit" onclick="submitForm();" >
<INPUT TYPE="button" value="Submit" onclick="submitForm();" >
do not use document.write
use document.getElementById("myID").innerHTML
a full working example like you wanted is that:
<HTML>
<HEAD>
<TITLE>Database Lookup</TITLE>
<script src="basic.js"></script>
</HEAD>
<BODY>
<H1>Database Lookup</H1>
<FORM action="javascript: submitForm();">
Please enter the ID of the publisher you want to find: <BR>
<INPUT TYPE="TEXT" id="id" NAME="id">
<BR>
<INPUT TYPE="SUBMIT" value="Submit" >
</FORM>
<script type="text/javascript">
function submitForm()
{
var idsearched=document.getElementById("id").innerHTML;
document.write("idsearched");
return false;
}
</script>
</BODY>
<HTML>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636667a4637436.html
评论列表(0条)