Javascript-从多个表单选择生成结果
我是新来的,编程也是新来的,我很少编写任何代码,但最近我一直想改善我的工作环境,我只想为我的同事创建一个简单的表单 简而言之,我已经创建了表单,我需要的只是将表单的所有结果生成到一个textarea中 现在我的问题是,我做了几种形式的“事件”,它们有不同的选择集,我想要的是生成的“事件”集的结果以及基本信息 这里是我的代码示例;开始JavascriptJavascript-从多个表单选择生成结果,javascript,forms,if-statement,multiple-choice,Javascript,Forms,If Statement,Multiple Choice,我是新来的,编程也是新来的,我很少编写任何代码,但最近我一直想改善我的工作环境,我只想为我的同事创建一个简单的表单 简而言之,我已经创建了表单,我需要的只是将表单的所有结果生成到一个textarea中 现在我的问题是,我做了几种形式的“事件”,它们有不同的选择集,我想要的是生成的“事件”集的结果以及基本信息 这里是我的代码示例;开始Javascript function generateresult() { name = document.FORM.namefromtextarea.value
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的结尾
如果用户单击了值为“Lost Person”的选项,则生成文本的结果将取自事件“type2”
所以我设法得到了name、phone和address的结果,但是对于lost item result,结果的值变成了“undefined”
所以。。。我怎样才能准确地编码这个?我甚至不确定我是否真的为我的简单表单做了正确的脚本/方法。。。提前感谢在我看来,您可能正在尝试创建一个变量,该变量从不远处的元素获取数据。您可能希望将代码放在另一个函数中
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
之类的词作为变量名,而是将其命名为lostWhere
或lostWhen
您尝试过使用两种表单吗?我认为这种方法可以使单个事件对应于特定的形式,使其更容易。var
不是可选的。您刚刚创建了一组有问题的全局变量,任何人都可以访问和修改。我计划根据将来的事件添加更多的事件类型,因此我想继续使用1页表单,该表单有多个选项,并基于多个事件生成结果。这样,我的同事就不必为不同类型的事件打开多个新页面,只需坚持使用包含所有事件的一个页面。