如何使用 JQuery 处理输入的选择进行计算并显示结果。
<input type="text" id="num1"/>
<select name="Calc">
<option value="add">ADD</option>
<option value="subtract">SUBTRACT</option>
<option value="multiply">MULTIPLY</option>
<option value="divide">DIVIDE</option>
</select>
<input type="text" id="num2"/>
<button id="Calculate">CALCULATE</button>
<button id="clear">CLEAR</button>
Calculated Value:
<div class="valueDisp">
<div id="calc_value">
</div>
</div>
$('input[name=Calc]').ready(function () {
var selected = $(this).val();
var num1 = $('#num1').val();
var num2 = $('#num2').val();
if (selected == 'add') var total = +num1 + +num2;
else if (selected == 'subtract') var total = num1 - num2;
else if (selected == 'multiply') var total = num1 * num2;
else if (selected == 'divide') var total = num1 / num2;
else var total = ("Please select an option");
$("#calculate").click(function () {});
$("#clear").click(function () {
$('.input').val("");
});
$("#calc_value").html(total);
});
我有以下内容:http: //jsfiddle.net/kHtHA/5/