0

我是新来的,我也是编程新手,我很少编写任何代码,但最近我一直想改善我的工作场所,我只想为我的同事创建一个简单的表单。

简而言之,我创建了表单,我需要的只是将表单的所有结果生成到 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 ;}

结束

如果用户单击了值为“Lost Person”的选项,则生成文本的结果将取自事件“type2”

所以我设法得到了姓名、电话和地址的结果,但是对于丢失的项目结果,结果的值变成了“未定义”。

那么......我究竟该如何编码呢?我什至不确定我是否真的为我的简单表单做正确的脚本/方法......提前谢谢

4

1 回答 1

0

在我看来,您可能正在尝试创建一个从不存在的元素中获取数据的变量。您可能希望将您拥有的代码放入另一个函数中。

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. 您也永远不要使用类似where或这样when的词作为变量名,而是将其命名为类似lostWherelostWhen

于 2013-03-05T19:36:24.987 回答