How to make a "submit" in javascript?

1

I am making a very simple game in html and javascript, the idea is that the user put the result of each operation, and if it is right, an alert appears for him. I wanted to use a table because I want to exercise this. The program is almost done, but I can not get the user to "submit" so I can check if it's right. I even tried to use a submit there, but I ended up doing a gambiarra that did not work. I searched on google and found nothing related. Right now ... J.G.

function olosko() {
  var x = document.getElementById("tabela").rows[0].cells[4];
  var y = document.getElementById("tabela").rows[1].cells[4];
  var z = document.getElementById("tabela").rows[2].cells[4];
  var r = document.getElementById("tabela").rows[3].cells[4];
  x.innerHTML = "<input type='text'>";
  y.innerHTML = "<input type='text'>";
  z.innerHTML = "<input type='text'>";
  r.innerHTML = "<input type='text'>";
  var a = document.getElementById("tabela").rows[0].cells[0];
  var b = document.getElementById("tabela").rows[0].cells[2];
  var c = document.getElementById("tabela").rows[1].cells[0];
  var d = document.getElementById("tabela").rows[1].cells[2];
  var e = document.getElementById("tabela").rows[2].cells[0];
  var f = document.getElementById("tabela").rows[2].cells[2];
  var g = document.getElementById("tabela").rows[3].cells[0];
  var h = document.getElementById("tabela").rows[3].cells[2];

  a.innerHTML = parseInt(Math.random()*10);
  b.innerHTML = parseInt(Math.random()*10);
  c.innerHTML = parseInt(Math.random()*10);
  d.innerHTML = parseInt(Math.random()*10);
  e.innerHTML = parseInt(Math.random()*10);
  f.innerHTML = parseInt(Math.random()*10);
  g.innerHTML = parseInt(Math.random()*10);
  h.innerHTML = parseInt(Math.random()*10);

  if(a+b==x && c-d==y && e/f==z && g*h==r){
    alert("VC GANHOU!");
  }
}
<button onclick="olosko()">ESTOU PRONTO!</button>
<table id="tabela" border=1 width=400 height=400>
  <tr>
    <td></td>
    <td>+</td>
    <td></td>
    <td>=</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>-</td>
    <td></td>
    <td>=</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>รท</td>
    <td></td>
    <td>=</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>X</td>
    <td></td>
    <td>=</td>
    <td><div id="olosko()"></div></td>
  </tr>
</table>
<submit><button>ACABEI!</button></submit>
    
asked by anonymous 21.09.2015 / 21:20

3 answers

2

You can use pure JavaScript to solve your problem by being very simple.

Example:

document.getElementById("myForm").submit();

With button:

<a href="#" onclick="document.getElementById('myForm').submit()">Submit Form</a>
    
21.09.2015 / 21:38
1

I made some modifications to your code, I used pure JavaScript like you.

var x;
var a, b;

function olosko() {
  a = document.getElementById("tabela").rows[0].cells[0];
  b = document.getElementById("tabela").rows[0].cells[2];
  x = document.getElementById("tabela").rows[0].cells[4];
  x.innerHTML = "<input id='val' type='text' />";

  a.innerHTML = parseInt(Math.random() * 10);
  b.innerHTML = parseInt(Math.random() * 10);
}

In this part of JavaScript, I first declared the variables "a, b and x" as global to be used in two functions. Follow the lines below for explanation:

a = document.getElementById("tabela").rows[0].cells[0];
a.innerHTML = parseInt(Math.random() * 10);

You used the same variable to create a column in your table, and to generate a random number, that's fine, but you did not assign the generated random number to any variable, not doing so, variable "a" will receive an object, which will be "HTMLTableCellElement". Here's what I did in the HTML part.

<button onclick="olosko()">ESTOU PRONTO!</button>
<table id="tabela" border=1 width=auto height=auto>
    <tr>
        <td id="a"></td>
        <td>+</td>
        <td id="b"></td>
        <td>=</td>
        <td><div id="olosko()"></div></td>
    </tr>
</table>
<button onclick="enviar()">ACABEI!</button>

I have created an "id" for the columns that receive the random number, then by JavaScript, pick up the numbers to create a condition and an action on the button "ACABEI!", to call the send () function that will make the assigning random numbers to a variable.

function enviar() {
    a = parseInt(document.getElementById("a").innerHTML);
    b = parseInt(document.getElementById("b").innerHTML);
    x = document.getElementById("val").value;

    if (a + b == x) {
        alert("VC GANHOU!");
    } else {
        alert("VC PERDEU!");
    }
}

Look closely at this line:

a = parseInt(document.getElementById("a").innerHTML);
It is here that the random number will be assigned the variables, and will replace the objects that were previously assigned, the "parseInt" had to be used because the random number was a "String" so it was necessary to convert to Number, different from the assignment of the variable "x" just above. About the condition has nothing to explain is the simplest.

I hope it helps, I started to study JavaScript soon, any questions are available.

    
22.09.2015 / 21:48
0

With the use of JQuery is more reliable, so that it runs smoothly in all browsers.

  $("#nome-do-meu-form").submit(function() {
                //alguma ação aqui ex :
                $("#btn").prop('disabled', true);
  });
    
21.09.2015 / 22:13