Click to See Complete Forum and Search --> : scripts are not working....can't find the error


digital_storm
11-01-2003, 01:49 PM
Hello!

I'm using JSP for the presentation and Jscript to check so the inputvalues!=null
I'm doing a questionnary, on my first page I check if the visitor owns 1)mobilephone, 2) a handhel computer and a 3)high 3G mobilphone. They get different questions depending on if they own 1), 2) and 3).

The scripts works nicely only if the visitor says that he/she owns 1) 3)....I can't figure it out why it doesen't work otherwise
Maybe is it because some of my "var" will not be on the page i.e if I don't own a mobilephone then the field "sk" will not be in the page. But I think that the "if(sek=="V\u00E4lj") will take care of that..or?
("u00E4","u00E5" and "u00F6" = unicode for swedish letters å ä ö)
Please can you help me?
The scripts are bellow, script A is on the page with question about mobilephones and script B is on the page about High 3G-phone

My formname/inputfields are correct

----------------------A-------------------------------

<script type="text/javascript">

function validate(){

var message ="\u00C5tg\u00E4rda f\u00F6ljande f\u00F6r att g\u00E5 vidare:"+"\n\n";
var korrekt=true;
var sek=document.element.sk.options[document.element.sk.selectedIndex].text;
var op=document.element.operator.options[document.element.operator.selectedIndex].text;
var spd=document.element.smsperdag.options[document.element.smsperdag.selectedIndex].text;
var amob=document.element.anvmob.options[document.element.anvmob.selectedIndex].text;
var wg=document.element.wapgprs.options[document.element.wapgprs.selectedIndex].text;

if(sek=="V\u00E4lj"){
message=message+"V\u00E4lj 'Separata' alternativt 'gemensam' terminal"+"\n";
korrekt=false;
}
if(op=="V\u00E4lj"){
message=message+"V\u00E4lj 'Operat\u00F5r'"+"\n";
korrekt=false;
}
if(spd=="V\u00E4lj"){
message=message+"V\u00E4lj 'Hur m\u00E5nga sms du skickar per dag'"+"\n";
korrekt=false;
}
if(amob=="V\u00E4lj"){
message=message+"V\u00E4lj 'Om du anv\u00E4nder din mobil mer i prvata sammanhang eller som ett arbetsredskap'"+"\n";
korrekt=false;
}
if(wg=="V\u00E4lj"){
message=message+"V\u00E4lj 'Om du har WAP/GPRS eller inte'"+"\n";
korrekt=false;
}




if(!korrekt){
alert(message);
return false;
}

if(korrekt){
return true;
}
}
------------------------------------------------------

-----------------------B------------------------------
<script type="text/javascript">

function validate(){

var message ="\u00C5tg\u00E4rda f\u00F6ljande f\u00F6r att g\u00E5 vidare:"+"\n\n";
var korrekt=true;
var f1=document.form1.tre7.options[document.form1.tre7.selectedIndex].text;
var f2=document.form1.tre9.options[document.form1.tre9.selectedIndex].text;
var f3=document.form1.tre10.options[document.form1.tre10.selectedIndex].text;
var f4=document.form1.tre11.options[document.form1.tre11.selectedIndex].text;
var f5=document.form1.treomj2.options[document.form1.treomj2.selectedIndex].text;

if(f1=="V\u00E4lj"){
message=message+"V\u00E4lj 'Uppdateringsfrekvens p\u00E5 informationsinneh\u00E5llet'"+"\n";
korrekt=false;
}
if(f2=="V\u00E4lj"){
message=message+"V\u00E4lj 'Om du vill ha GPS-funktion i kombination med tj\u00E4nster'"+"\n";
korrekt=false;

}
if(f3=="V\u00E4lj"){
message=message+"V\u00E4lj 'Om du vill ha en presentat\u00F6r som l\u00E4ser upp nyheterna likt som det g\u00F6rs i tv'"+"\n";
korrekt=false;

}
if(f4=="V\u00E4lj"){
message=message+"V\u00E4lj 'Om du vill ha en tj\u00E4nst d\u00E4r du kan skicka in texter, bilder och videosnuttar'"+"\n";
korrekt=false;

}
if(f5=="V\u00E4lj"){
message=message+"V\u00E4lj 'Vad du anser om 3's trov\u00E4rdighet som informationsf\u00F6rmedlare'"+"\n";
korrekt=false;

}



if(!korrekt){
alert(message);
return false;
}

if(korrekt){
return true;
}
}
------------------------------------------------------------------

Fang
11-02-2003, 07:16 AM
If some of the fields are not on the page,
then an error will occur when the var's are initialized.
i.e. if the field "sk" is not on the page then var sek=document. etc. will generate an error.

digital_storm
11-02-2003, 09:35 AM
Ok so I have to controll with booleans what the visitor have answered and then set up some if-statements with the "var statements in it".
Will it work then?

Fang
11-03-2003, 02:05 AM
Without seeing the full code it's difficult to know if it will work,
but it should work.
Often in cases where the contents of the form is unknown,
it is best to check the form using an index.
This function runs through the form named "myform", when a select element is found it branches depending on the name.
function Test() {
for(var i=0; i<window.document.myform.length; i++) {
if(window.document.myform.elements[i].type=="select-one") {
switch (window.document.myform.elements[i].name) {
case "sk" :
var sek="a value";
//do something
break;
case "operator" :
var op="a value;
//do something
break;
default :
alert("unknown select element: "+window.document.myform.elements[i].name);
}
}
}
}