2013-03-05 58 views
0

我在這裏新建,我在編程方面也很新,我也很少編寫任何代碼,但是最近我一直想改進我的工作場所,我只是想爲我創建一個簡單的表單同事。Javascript - 從多個表單選擇生成結果

爲了簡短起見,我創建了表單,我需要的只是將表單的所有結果生成到textarea中。

現在我的問題是,我做了幾種類型的「事件」的形式有不同的選擇,我想要的是這套「事件」的結果以及基本信息。

那麼,這裏是我的代碼示例;開始的Javascript

function generateresult() { 

name = document.FORM.namefromtextarea.value; 
phone = document.FORM.phonefromtextarea.value; 
address = document.FORM.addressfromtextarea.value; 

name2 = "Name: " + name + "\n"; 
phone2 = "Phone: " + phone + "\n"; 
address2 = "Address: " + address + "\n"; 

//problem type 1 

lostitem = document.FORM.lostitemfromtextarea.value; 
when = document.FORM.whenfromtextarea.value; 
where = document.FORM.wherefromtextarea.value; 

lostitem2 = "Lost Item?: " + lostitem + "\n"; 
when2 = "When?: " + when + "\n"; 
where2 = "Where?: " + where + "\n"; 

//problem type 2 

lostperson = document.FORM.lostpersonfromtextarea.value; 
personage = document.FORM.personagefromtextarea.value; 
personcloth = document.FORM.personclothfromtextarea.value; 

lostperson2 = "Person Name?: " + lostperson + "\n"; 
personage2 = "Age?: " + personage + "\n"; 
personcloth2 = "Wearing?: " + personcloth + "\n"; 

if (document.FORM.problemtype.value="Lost Item") 
{ 
eventtype = type1; 
} 
else if (document.FORM.problemtype.value="Lost Person") 
{ 
eventtype = type2; 
} 

type1 = lostitem2 + when2 + where2 ; 

type2 = lostperson2 + personage2 + personcloth2 ; 

document.FORM.generateresulttext.value = name2 + phone2 + address2 + eventtype ;} 

的JavaScript

的結束而如果用戶點擊了「失落的人」,選項有值生成的文本結果將事件「2型」

所以我採取我設法得到了名字,電話和地址的結果,但是對於丟失的結果,結果的值變成了「未定義」。

那麼...我該如何編碼呢?我甚至不知道,如果我真的做了正確的腳本/方法,我簡單的形式...在此先感謝

+1

你嘗試使用2種形式?我認爲這樣你可以讓單個事件對應一個特定的表單,使它更容易。 – KodeSeeker 2013-03-05 19:16:14

+0

'var'不是可選的。你只是創建了一堆有問題的全局變量,_anyone_可以訪問和修改。 – elclanrs 2013-03-05 19:18:45

+0

好,我打算根據未來的事件添加更多的事件類型,所以我要堅持1頁有多種選擇,並基於幾個事件產生結果的形式。這樣,我的同事們就不必爲其他種類的頁面打開幾個新頁面,只需要粘貼一頁就可以完成所有事件。 – SBJ 2013-03-05 19:24:02

回答

0

它看起來對我來說,你可能會想創建一個變量,從inexistant元素需要的數據。你可能想把你有的代碼放在另一個函數中。

function generateresult() { 

name = document.FORM.namefromtextarea.value; 
phone = document.FORM.phonefromtextarea.value; 
address = document.FORM.addressfromtextarea.value; 

name2 = "Name: " + name + "\n"; 
phone2 = "Phone: " + phone + "\n"; 
address2 = "Address: " + address + "\n"; 

//problem type 1 
function firstType(){ 

    lostitem = document.FORM.lostitemfromtextarea.value; 
    when = document.FORM.whenfromtextarea.value; 
    where = document.FORM.wherefromtextarea.value; 

    lostitem2 = "Lost Item?: " + lostitem + "\n"; 
    when2 = "When?: " + when + "\n"; 
    where2 = "Where?: " + where + "\n"; 

    document.FORM.generateresulttext.value = name2 + phone2 + address2 + lostitem2 + when2 + where2 ; 
} 

//problem type 2 
function secondType(){ 

    lostperson = document.FORM.lostpersonfromtextarea.value; 
    personage = document.FORM.personagefromtextarea.value; 
    personcloth = document.FORM.personclothfromtextarea.value; 

    lostperson2 = "Person Name?: " + lostperson + "\n"; 
    personage2 = "Age?: " + personage + "\n"; 
    personcloth2 = "Wearing?: " + personcloth + "\n"; 

    document.FORM.generateresulttext.value = name2 + phone2 + address2 + lostperson2 + personage2 + personcloth2 ; 
} 

if (document.FORM.problemtype.value="Lost Item") 
{ 
    firstType(); 
} 
else if (document.FORM.problemtype.value="Lost Person") 
{ 
    secondType(); 
} 

} 

未來,您不應該將數字放在變量的名稱中。你也不應該像在這裏那樣聲明變量。當你想創建一個變量時,你輸入var variableName = variableValue。你也從來不使用的話就像wherewhen一個變量名,而是將其命名爲類似lostWherelostWhen