我正在尝试使用jQuery制作计算器。如何从字符串执行数学运算?我需要在输出框中显示计算器操作。
$(document).ready(function() {
$("input[type='button']").click(function() {
var inputtt = $(this).val();
var displayinput = $("#getinput").val();
$("#getinput").val(displayinput + inputtt);
})
$("#submit").click(function() {
var displayinput = $("#getinput").val();
var output = parseInt(displayinput);
$("#output").val(displayinput);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="calcform" method="post" style="background:lightgreen; padding:10px; ">
<input type="text" id="output" value="" placeholder="OUTPUT" readonly><br><br>
<input type="text" id="getinput" value="" placeholder="INPUT"><br><br>
<input type="button" class="btn" value="-">
<input type="button" class="btn" value="+">
<input type="button" class="btn" value="*">
<input type="button" class="btn" value="/">
<input type="button" class="btn" value="c"><br><br>
<input type="button" class="btn" value="0">
<input type="button" class="btn" value="1">
<input type="button" class="btn" value="2">
<input type="button" class="btn" value="3">
<input type="button" class="btn" value="4"><br><br>
<input type="button" class="btn" value="5">
<input type="button" class="btn" value="6">
<input type="button" class="btn" value="7">
<input type="button" class="btn" value="8">
<input type="button" class="btn" value="9"><br><br>
<button type="button" id="submit">=</button>
</form>
答案 0 :(得分:0)
使用数学预定义对象,例如,如果我想方形
var y = Math.sqrt(16)
将在此处找到所有数学选项 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math