Check out the LimeSurvey source code on GitHub!

sumar campos numericos

6 years 1 week ago #56941 by jjcf
buen día, tengo unos campos numéricos y quiero ir sumando para que el usuario vea el total, se puede realizar un calculo dentro de un campo o alguna otra solución. Gracias!!!!

Please Log in to join the conversation.

5 years 11 months ago #58633 by rocio
Yo también intento hacer algo asi. Por lo que te he entendido, quieres ir sumando lo que va introduciendo en diferentes casillas e ir mostrandolo, de forma dinámica (se va actualizando), el total que va sumando. He encontrado esto por aqui, que debe hacer eso, lo único que el tipo de pregunta es una opción múltiple y yo lo busco para entradas numéricas.
<script language=Javascript>
// General definitions
// Define a regular expression to match the labels containing information about an answer
// In LimeSurvey, these labels start with "answer..."
var answerRegex = /^answer/;
// Find all answer checkboxes in the document and set their
// onchange events to updatePrice 
function Custom_On_Load()
	// Find all input elements
	var inputList=document.getElementsByTagName("input");	
	// Loop through each, looking for checkboxes
	var i;
	for( i=0; i< inputList.length;i++ )
		// If input item does not have an ID, skip
		if (! (inputList[i].id)) {continue;}
		// Skip to next if ID doesn't start with "answer"
		if (!answerRegex.test(inputList[i].id)) {continue;}
		// Skip to next if not a checkbox
		if (inputList[i].type.toUpperCase()!='CHECKBOX')  {continue;}
		// This is an answer checkbox!
		// Setup onchange event
		inputList[i].onclick = updatePrice;		
        // Load initial sum 
function updatePrice()
var labels=document.getElementsByTagName("LABEL");
// Define a regular expression to match a price inside parenthesis
// For example: ($29) or ($29.54).
// Set up 
priceRegex = /\(\$(\d*\.{0,1}\d+)\)/;
// Loop through all the labels, looking for labels corresponding to
// answers that have a price in them.
var i;
var total = 0;
for( i=0; i<labels.length;i++ )
	var myArray;	// Define array used for regex matching
	var inputID;	// Define field element
	// Get the ID of the field corresponding to the label using the 
	// "htmlFor" attribute
	// (FYI, there are some labels that will have to be screened out
	// because they don't correspond to an answer)
	// Does the label start with "answer"?
	// If not: exit.
	if (!answerRegex.test(labels[i].htmlFor)) {continue;}
	// First make sure this element exists
	// If it doesn't, go to the next element
	// If it does, it will be defined in inputID
	if (! (inputID = document.getElementById(labels[i].htmlFor)) ) {continue;}
	// See if the corresponding answer is a checkbox that is checked.
	// If not, go to next label
	if (inputID.type.toUpperCase()!='CHECKBOX')  {continue;}
	if (!inputID.checked) {continue;}
	// This is label for an answer.
	// The innerHTML will give us the text of the label
	// The price information will be in the form ($XX.XX)
	// which in contained in the label text.
	// Find a match for a decimal number inside the pattern "($" and ")"
	if ((myArray = priceRegex.exec(labels[i].innerHTML)) != null)
		// Keep a running tally
		total += parseFloat(myArray[1]);		
   // Update total price on form
   document.getElementById("totalPrice").value = "$" + formatCurrency(total);
function formatCurrency(num) {
   num = isNaN(num) || num === '' || num === null ? 0.00 : num;
   return parseFloat(num).toFixed(2);
Put the following code in the help section (or wherever you want the sum to appear):
Total Cost for selected accessories:&nbsp;<input type="readonly" value="" id="totalPrice" />

Encontrado en:

¿Alguien sabe como hacerlo para entradas numéricas?

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form