2012-05-08 17 views
5

Me preguntaba cómo podría ingresar un valor en un formulario con html. Entonces, cuando el usuario va al sitio, ya hay números en los cuadros de texto y usan esos números para jugar y "agregar juego". Esto es lo que tengo hasta ahora:JavaScript - escribiendo valor en formato html desde el script

 <script type="text/javascript"> 

    function addSubmit() { 
//trying to have num1 and num2 write into the html form 
    var num1= Math.floor(89+Math.random()*11); 
    var num2 = Math.floor(89+Math.random()*11); 
    /*num1 = value1 
    num2 = value2*/ 
    document.getElementById("value1").value = num1; 
    document.getElementById("value2").value = num2; 

    var guess = document.getElementById("submit").value; 
    var answer = num1 + num2; 
    //have to write this to main window 
    if (guess == answer) 
    document.writeln("Congratulations you answered correctly! The answer to: "+num1+"+"+num2+"= "+answer); 
    else 
    document.writeln("Nice try, but the correct answer to: "+num1+"+"+num2+"= "+answer); 
    } 

    function addGiveUp() { 
    var num1 = Math.floor(89+Math.random()*11) 
    var num2 = Math.floor(89+Math.random()*11) 
    document.addition.value1.value=num1; 
    document.addition.value2.value=num2; 
    var guess = document.getElementById("submit").value; 
    var answer = (document.getElementById("value1").value) + (document.getElementById("value2").value); 
    document.writeln("Never give up! The answer to: "+num1+"+"+num2+"= "+answer); 
    } 
    </script> 
    <form name="addition" action="" method="get"> 
    <table border="1"> 
      <tr> 
     <td>Value 1:</td> <td><input type="text" name="value1" id="value1"/></td> 
       </tr> 
      <tr> 
     <td>Value 2:</td> <td><input type="text" name="value2" id="value2"/></td> 
       </tr> 
      <tr> 
     <td>Answer:</td> <td><input type="text" id="answer" /></td> 
       </tr> 
      <tr><td><input type="button" value="Submit" onClick="addSubmit()" id="submit" /></td><td><input type="button" value="Give Up" onClick="addGiveUp()" /></td></tr> 
      </table> 
    </form> 

Agradezco cualquier ayuda! ¡Gracias!

+1

¿cuál es el problema? ¿Dónde estás atrapado? es esta tarea? ¿Estás buscando una revisión? Bienvenido a SO – rlemon

+0

Es tarea. Estoy atascado en cómo escribir valores en el formulario html que tengo en el código. Quiero utilizar el código de JavaScript para escribir en el formulario sin tener que mover el formulario al script y ¡gracias! – Gcap

Respuesta

3

así se puede poner este script después de crear el formulario:

<form name="addition" action="" method="get"> 
      <table border="1"> 
        <tr> 
       <td>Value 1:</td> <td><input type="text" name="value1" id="value1"/></td> 
         </tr> 
        <tr> 
       <td>Value 2:</td> <td><input type="text" name="value2" id="value2"/></td> 
         </tr> 
        <tr> 
       <td>Answer:</td> <td><input type="text" id="answer" /></td> 
         </tr> 
        <tr><td><input type="button" value="Submit" onClick="addSubmit()" id="submit" /></td><td><input type="button" value="Give Up" onClick="addGiveUp()" /></td></tr> 
        </table> 
</form> 

<!-- This is the script--> 
<script type="text/javascript"> 
     document.getElementById("value1").value = Math.floor(89+Math.random()*11); 
     document.getElementById("value2").value = Math.floor(89+Math.random()*11);​ 
    </script> 

que genere números aleatorios para usted y ponerlos en forma Vea aquí el código: http://jsfiddle.net/wVAFt/

+0

Esto funcionó! Gracias :) – Gcap

+0

Lo único es que ahora la variable de respuesta está siendo num1 y num2 juntos. A veces odio la programación -_- – Gcap

+0

¿Qué quieres decir con que se rompieron? – adedoy

0
<input type="text" id="answer" value="5" > 

or 

document.getElementById("answer").value = "5"; 
+0

Gracias, pero el problema es que quiero escribir el valor de una variable en el formulario. Eso funcionó para mí al reemplazar 5 con el nombre de la variable, excepto que tengo que presionar Enviar y luego presionar el botón Atrás para ver los valores de las variables en los cuadros de texto – Gcap

+0

Puede hacer que un botón no envíe el formulario pero desencadene algún tipo de actualización! –

0

Para tener números en los campos de entrada, puede configurarlos para que tengan un valor, como value="10", y el valor predeterminado sería 10 hasta que el usuario lo cambie.

Cuestiones relacionadas