2016-05-21 20 views
0
<!DOCTYPE html> 
<head><title> test ! </title> 
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" /> 
<link rel="stylesheet" type="text/css" href="css/index.css" /> 
</head> 
<html> 
<body> 

<div class="container"> 

first number : <input class="form-control" id="num1" /> 
<br> 
second number : <input class="form-control" id="num2" /> 
<br> 
<select id="mathtype"> 
    <option value="add"> addition </option> 
    <option value="sub"> subtraction </option> 
    <option value="mul"> multiplication </option> 
</select> 
<br><br> 
<button class="btn btn-primary" onclick="submit()" > submit </button> 
<br><br> 
<input type="text" id="output" > 

</div> 



</body> 
<script src="js/jquery-1.9.1.js"></script> 
<script> 

function submit(){ 

    var mathtype = document.getElementById['mathtype']; 

    if (mathtype == add) { 
     return num1 + num2; 
    } else if (mathtype == sub) { 
     return num1 - num2; 
    } else if (mathtype == mul) { 
     return num1 * num2; 
    } 

    return true; 

} 

</script> 

</html> 

Mein aktueller Fehler:Wie kann ich zwei Variablen mithilfe von JavaScript addieren, subtrahieren oder multiplizieren?

SyntaxError: function statement requires a name.

Ich möchte ein Programm machen, die eine mathematische Operation entsprechend dem gewählten Wert ausführt (hinzufügen, sub, mul) und nach einreichen klicken, um die Antwort wird in der Eingabe mit einer ID "output" angezeigt.

+0

Denken Sie daran, dass Sie mit einem Minimal, Komplett stellen sollten, und prüfbare Beispiel (http: // Stackoverflow .com/hilfe/mcve). In diesem Fall ist der größte Teil des HTML nicht erforderlich. – josecortesp

+0

sorry sir josecortes hehe seit im neu in Javascript im Annahme, dass die Erklärung js Skript könnte eines der Probleme hehe – expert123

+0

Keine Notwendigkeit, sich zu entschuldigen. Wir sind alle hier, um zu lernen und zu helfen – josecortesp

Antwort

1

function submit() { 
 
    var mathtype = document.getElementById('mathtype').value; 
 
    //Get the value of the select element..Correct the typo([]) 
 
    var num1 = Number(document.getElementById('num1').value); 
 
    //Get the value of `num1` input and convert it to type `Number` 
 
    var num2 = Number(document.getElementById('num2').value); 
 
    //Get the value of `num2` input and convert it to type `Number` 
 
    if (mathtype == 'add') { 
 
    //Compare with `string` as value of select input will be of type string, you did not have variable `add` to be tested 
 
    document.getElementById('output').value = num1 + num2; 
 
    } else if (mathtype == 'sub') { 
 
    //Compare with `string` as value of select input will be of type string, you did not have variable `sub` to be tested 
 
    document.getElementById('output').value = num1 - num2; 
 
    } else if (mathtype == 'mul') { 
 
    //Compare with `string` as value of select input will be of type string, you did not have variable `mul` to be tested 
 
    document.getElementById('output').value = num1 * num2; 
 
    } 
 
}
<div class="container"> 
 
    first number : 
 
    <input class="form-control" id="num1" /> 
 
    <br>second number : 
 
    <input class="form-control" id="num2" /> 
 
    <br> 
 
    <select id="mathtype"> 
 
    <option value="add">addition</option> 
 
    <option value="sub">subtraction</option> 
 
    <option value="mul">multiplication</option> 
 
    </select> 
 
    <br> 
 
    <br> 
 
    <button class="btn btn-primary" onclick="submit()">submit</button> 
 
    <br> 
 
    <br> 
 
    <input type="text" id="output"> 
 
</div>

Hinweis: Gehen Sie durch die Kommentare für detaillierte explaination

+0

sir rayon vielen Dank für die schnelle Antwort :) – expert123

+1

@ expert123, glücklich zu helfen! Bitte [akzeptieren und up-stimme] (http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow) die beste Lösung, die den Zweck gelöst hat :) __Happy Kodierung! __ – Rayon

Verwandte Themen