2016-05-21 1 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> 

私の現在のエラー:JavaScriptを使用して2つの変数を追加、減算、または掛け算するにはどうすればよいですか?

SyntaxError: function statement requires a name.

私が選択した値に応じた数学演算を実行するプログラムを作りたいのですが(サブ、MUL、追加)、そして、答えを提出クリックした後id "output"で入力に表示されます。

+0

のコメントを通過します.com/help/mcve)。この場合、ほとんどのHTMLは必須ではありません。 – josecortesp

+0

申し訳ありませんjosecortes hehe以来、私はjavascriptで新しい私は宣言するjsスクリプトは、hehe問題の一つかもしれないと仮定 – expert123

+0

謝罪する必要はありません。私たちは皆、ここで学び、助けるためにここにいます – josecortesp

答えて

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>

:// stackoverflowの:あなたは、HTTP(、最小完全、かつ検証例で尋ねるべきであることを覚えておいてください詳細、について説明

+0

サーレーヨンは速い返信のためにそんなに感謝します:) – expert123

+1

@ expert123、お手伝いをしましょう!親切に[受諾とアップ - 投票](http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow)目的を解決した最高の解決策:) __Happyコーディング!__ – Rayon

関連する問題