Welcome, Guest
Username: Password: Remember me

TOPIC: sumar campos numericos

sumar campos numericos 3 years 7 months ago #56941

  • jjcf
  • jjcf's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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!!!!
The administrator has disabled public write access.

Re: sumar campos numericos 3 years 6 months ago #58633

  • rocio
  • rocio's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 34
  • Karma: 0
Hola,
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 
	updatePrice();
}
 
 
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);
}
 
</script>
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: docs.limesurvey.org/tiki-index.php?page=..._etc._in_LimeSurvey_


¿Alguien sabe como hacerlo para entradas numéricas?
The administrator has disabled public write access.
Time to create page: 0.218 seconds
Donation Image