Themen-Optionen Ansicht
Alt 17.04.2012, 16:32   #1
JensL
Administrator
Site Admin
 
Registriert seit: 02.03.2010
Beiträge: 2.470
JensL befindet sich auf einem aufstrebenden Ast
How can I sum values on a page in real-time?
You want the user to insert digits in open arrays in your survey. The sum of the values should not exceed a certain number. At the same time, you want to show the final result and the changes after each entry that was made by the user.
First of all you have to implement the following java script into your survey with the help of the question types 998 and 911. Since you have to create the question, where the user inserts the digits with the question type 911, you should use this type.
To keep things simple you should create the page with the standard question types and change the type to 911 later. This way you will get the right html code.

Please notice that the functional efficiency of the script depends on configuration and technical infrastructure of your system (e.g. operating system, browser, software version).


Code:
<script language="JavaScript"> <!-- var max=100; function ViewSum(SumId) { var nCounter; var nSum=0; var MyInputCollection=document.getElementsByTagName("input"); for (nCounter=0;nCounter<MyInputCollection.length;nCounter++) { if (MyInputCollection[nCounter].getAttribute("Id")==SumId) { if (MyInputCollection[nCounter].value!="" && parseInt(MyInputCollection[nCounter].value) != NaN) { nSum += parseInt(MyInputCollection[nCounter].value); } else { nSum = nSum; } } } if(nSum <= max) { document.getElementById("Result").innerHTML=nSum; } } --> </script>
The variable max defines the maximum value the user can enter.
After that you have to adapt the fields were the summand is entered later. Every field (input-tag) has to run the javascript function from above and needs to get an ID.

Code:
<input type="text" name="v_138" value="" size="3" maxlength="3" >
changes to

Code:
<input type="text" onblur="ViewSum('SumA');" Id="SumA" name="v_138" value="" size="3" maxlength="3" >
One inserts the javascript function ViewSum(‘SumA’) into the tag and gives it an ID. After that, the given ID has to be referred to the javascript function call. ID and parameter have to be the same.
The IDs must be identical for every array that will calculate the sum later.
Last but not least you have to define an area where the sum will be shown. To do this you have to add a new cell into the table and give it the ID “result”. This cell will contain the result.
For example:

Code:
<td Id="Result"></td>
We recommend predefining the input fields. The code for this example looks like this:

Code:
<script language="JavaScript"> <!-- var max=100; function ViewSum(SumId) { var nCounter; var nSum=0; var MyInputCollection=document.getElementsByTagName("input"); for (nCounter=0;nCounter<MyInputCollection.length;nCounter++) { if (MyInputCollection[nCounter].getAttribute("Id")==SumId) { if (MyInputCollection[nCounter].value!="" && parseInt(MyInputCollection[nCounter].value) != NaN) { nSum += parseInt(MyInputCollection[nCounter].value); } else { nSum = nSum; } } } if(nSum <= max) { document.getElementById("Result").innerHTML=nSum; } } --> </script> <table border="0" cellspacing=0 cellpadding=0 > <tr> <td>The eye test: </div></td> <td> <input type="text" onblur="ViewSum('SumA');" Id="SumA" name="v_138" value=" #v_138#" size="3" maxlength="3" >points </td> <td></td> </tr> <tr> <td>Purchasing eye correcting glasses:</td> <td> <input type="text" onblur="ViewSum('SumA');" Id="SumA" name="v_139" value=" #v_139#" size="3" maxlength="3" >points </td> <td></td> </tr> <tr> <td> <b>CURRENT TOTAL:</b></td> <td Id="Result"></td> <td></td> </tr> </table>
JensL ist offline  
 


Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge anzufügen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

vB Code ist An.
Smileys sind An.
[IMG] Code ist An.
HTML-Code ist Aus.
Gehe zu


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:28 Uhr.

unipark
© Copyright 2011 QuestBack GmbH - Unipark Forum Onlineforschung | Unipark Online-Umfrage Tool | kontakt | impressum |