最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to handle HTML <forms> with javascript - Stack Overflow

programmeradmin1浏览0评论

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", just NAME="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
Add a ment  | 

3 Answers 3

Reset to default 1

The 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>
发布评论

评论列表(0)

  1. 暂无评论