前端代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <input id="txtNum1" name="txtNum1" type="text" width="130" /> 2 <select id="fh" name="fh"> 3 <option id="jia" value="+">+</option> 4 <option id="jian" value="-">-</option> 5 <option id="chen" value="*">*</option> 6 <option id="chu" value="/">/</option> 7 </select> 8 <input id="txtNum2" name="txtNum2" type="text" width="130" /> 9 <input id="btnSubmit" type="button" value="=" /> 10 <input id="txtResult" type="text" width="130" /> 11 12 @section scripts{ 13 <script> 14 $(function () { 15 $("#btnSubmit").bind("click", function () { 16 debugger 17 $.ajax({ 18 @*url: '@Url.Action("JS")',*@ 19 url: "/Home/JS", 20 data: { 21 "num1": $("#txtNum1").val(), 22 "num2" : $("#txtNum2").val(), 23 "fh":$("#fh").val() 24 }, 25 dataType: "json", 26 type: "post", 27 success: function (data){ 28 $("#txtResult").val(data.Result); 29 }, 30 error: function (e, a){ 31 alert(a); 32 } 33 }) 34 }) 35 }) 36 </script> 37 }
后台action代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [HttpPost] 2 public JsonResult JS(string num1, string num2,string fh) 3 { 4 int result = 0; 5 try 6 { 7 switch (fh) 8 { 9 case "+": 10 result = Convert.ToInt32(num1) + Convert.ToInt32(num2); break; 11 case "-": 12 result = Convert.ToInt32(num1) - Convert.ToInt32(num2); break; 13 case "*": 14 result = Convert.ToInt32(num1) * Convert.ToInt32(num2); break; 15 case "/": 16 result = Convert.ToInt32(num1) / Convert.ToInt32(num2); break; 17 default: 18 break; 19 } 20 } 21 catch (Exception ex) 22 { 23 log.Error("Error", new Exception(ex.Message)); 24 return Json(""); 25 } 26 var data = new { Result = result }; 27 return Json(data); 28 }