標籤:control value cal 返回 jquer public text options btn
一、非同步兩種方法
- 用jQ的非同步 返回content
- controllor:return Content(sum.ToString());//這裡涉及到一個自動封裝的問題
- html頁:<form id="form1">
<input type="text" name="calc1" />+
<input type="text" name="calc2" />
<input type="button" id="btnAdd" value="加" />
<input type="text" name="sum" id="sum" />
</form>
- $(function () {
$(‘#btnAdd‘).click(function () {
$.post(
‘@Url.Action("CalcAdd","Home")‘,//提交到的地址
$(‘#form1‘).serialize(),//把表單的資料序列化發送
function (msg) {//回呼函數,msg是接收到的伺服器端發來的資料
$(‘#sum‘).val(msg);
}
)
})
})
2.AjaxHelper類
- controller代碼
- public ActionResult CalcAdd1(int calc1, int calc2)
- return Json(temp, JsonRequestBehavior.AllowGet);
- html代碼
- @using (Ajax.BeginForm("CalcAdd1", "home", new AjaxOptions()
{
OnSuccess = "Success"
}))
{
<input type="text" name="calc1" />
<span>+</span>
<input type="text" name="calc2" />
<input type="submit" value="=" />
<input type="text" id="result" />
}
- 調用的指令碼
- <script src="~/jquery-1.8.3.min.js"></script>
<script src="~/jquery.unobtrusive-ajax.min.js"></script>
MVC入門第一天