質問

Im new here, Im new in programming as well and I seldom code anything, but recently I've always wanted to improve my work place and I just want to create a simple form for my colleagues.

To make things short, I've created the form, what I need is just generating all the result of the form into a textarea.

Now my problem is, I've made several type of "Event" in the form that have different set of choices, and what I want is the result of that set of "Event" generated as well with the basic information.

Well, here example of my code; Begin 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 ;}

End of javascript

And if a user clicked option that have value "Lost Person", the result for generated text will be taken from event "type2"

So I've managed to get the result for name, phone, and address, but as for lost item result, the value of the result became "undefined".

So... how exactly can I code this? I am not even sure if Im really doing the right script / method for my simple form... Thanks in advance

役に立ちましたか?

解決

It looks to me like you might be trying to create a variable that takes data from an inexistant element. You might want to put the code you have inside another function.

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();
}

}

In the future, you should NOT put numbers in a variable's name. You should also NEVER declare variables like you're doing here. When you want to create a variable, you type var variableName = variableValue. You also NEVER use words like where or when for a variable name, but instead name it to something like lostWhere or lostWhen.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top