2011-05-12 237 views
0

的JavaScript:表單提交

function CreateMsg() { 
    var MsgDOM = document.getElementById("MSG"); 
    MsgDOM.innerHTML = "Hello, " + document.forms[0].FNAME.value + " " + document.forms[0].LNAME.value + ". You're sex is " + document.forms[0].GENDER.value; 
} 

HTML:

<FORM NAME="DUH"> 
    <INPUT TYPE=TEXT NAME=FNAME> 
    <INPUT TYPE=TEXT NAME=LNAME> 
    <SELECT NAME="GENDER"> 
     <OPTION VALUE="Male">Male</OPTION> 
     <OPTION VALUE="Female">Female</OPTION> 
    </SELECT> 
    <BR><BR> 
    <input type="button" value="Search" onclick="javascript:CreateMsg();"/> 
    <BR><BR> 
    <SPAN ID="MSG">&nbsp;</SPAN> 
</FORM> 
+0

我只是猜測。您的JS功能無法正常工作,您需要使用ID屬性而不是NAME,同時在HTML中命名您的INPUT。 – 2011-05-12 04:38:04

回答

0

請更新JavaScript方法是這樣 -

function CreateMsg() { 
    var MsgDOM = document.getElementById("MSG"); 
    var duhForm = document.forms[0]; 
    MsgDOM.innerHTML = "Hello, " + duhForm.FNAME.value + " " + duhForm.LNAME.value + ". You're sex is " + duhForm.GENDER.value; 
    duhForm.target="_self"; // or _top or _parent or _blank 
    duhForm.action="PleaseAskTheQuestion.do"; 
    duhForm.submit(); 
} 

已經提交過短表單在同一頁面上,但服務器太忙;沒有甜甜圈給你。 :-)