Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Svuotare campi

  1. #1

    Svuotare campi

    Salve, sto progettando una calcolatrice con JavaScript molto basilare (ho appena iniziato ad utilizzare questo linguaggio di programmazione) e dovrei far si che ogni volta che il calcolo venga eseguito i due campi dove inserisco i numeri si svuotino automaticamente. La calcolatrice è molto banale e va bene così, è possibile farlo?? Vi metto qui sotto il codice....
    codice:
    <!DOCTYPE html>
    <html>
    <body>
    
    
    <h2>CALCULATOR</h2>
    
    
    <p>Please input two numbers</p>
    
    
    <input id="numb">
    
    
    <button type="button" onclick="myFunction()">Submit</button>
    
    
    <p id="demo"></p>
    
    
    <script>
    function myFunction() {
      var x, text;
    
    
      // Get the value of the input field with id="numb"
      x = document.getElementById("numb").value;
    
    
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x)) {
        text = "Input not valid";
      } else {
        text = "Input OK";
      }
      document.getElementById("demo").innerHTML = text;
    }
    </script>
    
    
    
    
    
    
    
    
    <input id="numb1">
    
    
    <button type="button" onclick="myFunction1()">Submit</button>
    
    
    <p id="demo1"></p>
    
    
    <script>
    function myFunction1() {
      var x1, text1;
    
    
      // Get the value of the input field with id="numb"
      x1 = document.getElementById("numb").value;
    
    
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x1)) {
        text = "Input not valid";
      } else {
        text = "Input OK";
      }
      document.getElementById("demo1").innerHTML = text;
    }
    </script>
    
    
    
    
    
    
    
    
    <button type="button" onclick="myFunction2()">+</button>
    <p id="demo2"></p>
    <script>
    function myFunction2() {
      var x,y,z;
     
    
    
      // Get the value of the input field with id="numb"
      x = document.getElementById("numb").value;
      y = document.getElementById("numb1").value;
      z = 0;
    
    
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x)) {
       
      } else {
        z= (x + y);
      }
      document.getElementById("demo2").innerHTML = z;
    }
    </script>
    
    
    <button type="button" onclick="myFunction2()">-</button>
    <p id="demo2"></p>
    <script>
    function myFunction2() {
      var x,y,z;
     
    
    
      // Get the value of the input field with id="numb"
      x = document.getElementById("numb").value;
      y = document.getElementById("numb1").value;
      z = 0;
    
    
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x)) {
       
      } else {
        z= (x - y);
      }
      document.getElementById("demo2").innerHTML = z;
    }
    </script>
    
    
    <button type="button" onclick="myFunction2()">*</button>
    <p id="demo2"></p>
    <script>
    function myFunction2() {
      var x,y,z;
     
    
    
      // Get the value of the input field with id="numb"
      x = document.getElementById("numb").value;
      y = document.getElementById("numb1").value;
      z = 0;
    
    
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x)) {
       
      } else {
        z= (x * y);
      }
      document.getElementById("demo2").innerHTML = z;
    }
    </script>
    
    
    
    
    <button type="button" onclick="myFunction2()">/</button>
    <p id="demo2"></p>
    <script>
    function myFunction2() {
      var x,y,z;
     
    
    
      // Get the value of the input field with id="numb"
      x = document.getElementById("numb").value;
      y = document.getElementById("numb1").value;
      z = 0;
    
    
      // If x is Not a Number or less than one or greater than 10
      if (isNaN(x)) {
       
      } else {
        z= (x / y);
      }
      document.getElementById("demo2").innerHTML = z;
    }
    </script>
    
    
    
    
    </body>
    Ultima modifica di ciro78; 26-04-2020 a 11:13 Motivo: tag code

  2. #2
    Moderatore di CSS L'avatar di KillerWorm
    Registrato dal
    Apr 2004
    Messaggi
    5,776
    Ciao è benvenuto su questo forum, puoi riproporre il codice in modo fruibile? Vedi l'uso dei tag CODE nel regolamento di sezione.

    In questo caso però potrebbe essere più utile creare un esempio usando un qualche playground (ad es. JSFiddle o CodePen o SoloLearn ).
    In questo modo sarà più semplice aiutarti.

    Buona permanenza sul forum
    Installa Forum HTML.it Toolset per una fruizione ottimale del Forum

  3. #3
    Moderatore di Javascript L'avatar di ciro78
    Registrato dal
    Sep 2000
    residenza
    Napoli
    Messaggi
    8,514
    Ciao max,
    benvenuto sul forum. Ti invito, qualora non lo avessi già fatto, a leggere il regolamento e in particolare la sezione dei tag code.
    Ciro Marotta - Programmatore JAVA - PHP
    Preferisco un fallimento alle mie condizioni che un successo alle condizioni altrui.


Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.