2010-10-17 6 views
2

J'essaie de faire est d'obtenir toute la valeur de 10 textbox et y arriver au total et l'imprimer sur soumettre. J'ai fait var id = getelementbyid (id) puis x = var1 + var2 mais rien ne se passe. Aidez-moi s'il vous plaît à ce sujet.résumer les valeurs des entrées

 
<script type="text/javascript"> 
    function test(){ 
    document.getElementById('out').style.display = "block"; 
    return false; 
    } 
    </script> 
<form action="" onsubmit="return test()"> 
    <div class="selftest"> 
    <p><input type="text" size="1" id="text1" /><label> I Love Animals</label></p> 
    <p><input type="text" size="1" id="text2" /><label> I Pay Close Attention To Details</label></p> 
    <p><input type="text" size="1" id="text3" /><label> I Communicate Well With Others</label></p> 
    <p><input type="text" size="1" id="text4" /><label> I Am Very Dependable</label></p> 
    <p><input type="text" size="1" id="text5" /><label> I Get Along Well With Others</label></p> 
    <p><input type="text" size="1" id="text6" /><label> I Am In Good Physical Shape</label></p> 
    <p><input type="text" size="1" id="text7" /><label> I Understand I May Be Bitten</label></p> 
    <p><input type="text" size="1" id="text8" /><label> I Handle Stress Well</label></p> 
    <p><input type="text" size="1" id="text9" /><label> I Am Well Organized</label></p> 
    <p><input type="text" size="1" id="text10" /><label> I Am Intelligent</label></p> 
    <p><input type="submit" value="Submit Answers" /></p> 
    </div> 
    </form> 
<div id="out" style="display:none;"> 
    Here are the results of your Veterinary Technician Self Test: &lt;result&gt; Obviously veterinary technicians must love animals, but they must love to be around and communicate with people also because they spend much of their time interacting with co-workers and pet owners. An individual should be able to take a leadership role and be able to talk with strangers, to be a good candidate for a veterinary technician career. Those who want to become veterinary technicians must have good communication skills and have the ability to work well with others. They must also be well-organized and be able to pay attention to detail. They must be of above average intelligence and have a good understanding of math and science. It is necessary that a veterinary technician be dependable since an animal’s life may rest in their hands when treatments must be delivered in a timely manner. A veterinary technician’s job can be very demanding. He or she must be able to spend hours on their feet and have the ability to lift and restrain animals. Unfortunately, being bitten is part of the job. It does not occur often, but a veterinary technician must realize that it is a real possibility. Due to the possibility of emergency situations, a veterinary technician must be able to handle stress well. From the results of the self test, you have many of the characteristics needed to work in this field, but you may need to do some more research before you go any further. Start by reading <a href="http://www.vettechuniversity.com/veterinary-technician-career-information/" target="_blank"><strong>Veterinary Technician Career Information</strong></a>. 
    </div> 

S'il vous plaît poser des questions si vous ne comprenez pas ma question :)

+0

Je ne vois pas où vous essayez d'accéder aux zones de texte? –

+0

Je supprime le getelementbyid pour la zone de texte parce que c'est inutile. Et j'essaye de mettre la valeur sur

result
Jorge

Répondre

1

Voici ma version - vous pouvez mélanger et assortir, mais il y a au moins une amélioration dans la mienne (la durée)

<script type="text/javascript"> 
var nofQuestions = 10; // or scan the complete form as already posted 
function test(theForm) { 
    var total=0; 
    for (var i=1;i<=nofQuestions;i++) { // or use document.getElementById and retain the IDs 
    var val = theForm.elements["text"+i].value; 

    total+= (isNaN(val) || !val)?0:parseInt(val,10); 
    } 
    document.getElementById('result').innerHTML=total; 
    document.getElementById('out').style.display = "block"; 
    return false; 
} 
</script> 
<form action="" onsubmit="return test(this)"> 
    <div class="selftest"> 
    <p><input type="text" size="1" name="text1" value="" /><label> I Love Animals</label></p> 
    <p><input type="text" size="1" name="text2" value="" /><label> I Pay Close Attention To Details</label></p> 
    <p><input type="text" size="1" name="text3" value="" /><label> I Communicate Well With Others</label></p> 
    <p><input type="text" size="1" name="text4" value="" /><label> I Am Very Dependable</label></p> 
    <p><input type="text" size="1" name="text5" value="" /><label> I Get Along Well With Others</label></p> 
    <p><input type="text" size="1" name="text6" value="" /><label> I Am In Good Physical Shape</label></p> 
    <p><input type="text" size="1" name="text7" value="" /><label> I Understand I May Be Bitten</label></p> 
    <p><input type="text" size="1" name="text8" value="" /><label> I Handle Stress Well</label></p> 
    <p><input type="text" size="1" name="text9" value="" /><label> I Am Well Organized</label></p> 
    <p><input type="text" size="1" name="text10" value="" /><label> I Am Intelligent</label></p> 
    <p><input type="submit" value="Submit Answers" /></p> 
    </div> 
    </form> 
<div id="out" style="display:none;"> 
    Here are the results of your Veterinary Technician Self Test: <span id="result"></span> Obviously veterinary technicians must love animals, but they must love to be around and communicate with people also because they spend much of their time interacting with co-workers and pet owners. An individual should be able to take a leadership role and be able to talk with strangers, to be a good candidate for a veterinary technician career. Those who want to become veterinary technicians must have good communication skills and have the ability to work well with others. They must also be well-organized and be able to pay attention to detail. They must be of above average intelligence and have a good understanding of math and science. It is necessary that a veterinary technician be dependable since an animal’s life may rest in their hands when treatments must be delivered in a timely manner. A veterinary technician’s job can be very demanding. He or she must be able to spend hours on their feet and have the ability to lift and restrain animals. Unfortunately, being bitten is part of the job. It does not occur often, but a veterinary technician must realize that it is a real possibility. Due to the possibility of emergency situations, a veterinary technician must be able to handle stress well. From the results of the self test, you have many of the characteristics needed to work in this field, but you may need to do some more research before you go any further. Start by reading <a href="http://www.vettechuniversity.com/veterinary-technician-career-information/" target="_blank"><strong>Veterinary Technician Career Information</strong></a>. 
    </div> 
+0

merci beaucoup ... (y) – Jorge

1

Si vous voulez calculer la somme de toutes les valeurs que vous devrez d'abord les convertir en entier:

var total = 0; 
for (var i = 1; i <= 10; i++) { 
    // get the DOM element by id 
    var element = document.getElementById('text' + i); 
    if (element != null) { 
     // if the element is found try parsing it's value to integer 
     var value = parseInt(element.value); 
     if (!isNaN(value)) { 
      // if the parsing was successful add to the total amount 
      total = total + value; 
     } 
    } 
} 
alert(total); 

Référence des fonctions utiles:

+0

Bien que n'importe quel nombre l'utilisateur saisisse est plus grand que 7, je ferais que 'parseInt (element.value, 10)'. Dans le cas contraire, vous rencontrerez des problèmes lorsqu'un utilisateur tape "08" ou "010" et que l'ordinateur l'interprète comme * octal * plutôt que comme décimal. – PleaseStand

Questions connexes