jquery自動將form表單封裝成json的具體實現

來源:互聯網
上載者:User

前端頁面:
複製代碼 代碼如下:
<span style="font-size:14px;"> <form action="" method="post" id="tf">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<th>
姓名:
</th>
<td>
<input type="text" id="txtUserName" name="model.UserName" />
</td>
<th>
聯絡手機:
</th>
<td>
<input type="text" name="model.Mobile" id="txtMobile" maxlength="11" />
</td>
<th>
密碼:
</th>
<td>
<input type="password" name="model.Pwd" id="txtPwd" maxlength="11" />
</td>
</tr>
<tr>
<td style="text-align: center;" colspan="2">
<input type="button" value=" 提 交 " style="padding-top: 3px;" name="butsubmit"
id="butsubmit" />
</td>
</tr>
</table>
</form>
//提示到伺服器
$(function () {
$("#butsubmit").click(function () {
var data = $("#tf").serializeArray(); //自動將form表單封裝成json


// $.ajax({
// type: "Post", //訪問WebService使用Post方式請求
// contentType: "application/json", //WebService 會返回Json類型
// url: "/home/ratearticle", //調用WebService的地址和方法名稱組合 ---- WsURL/方法名
// data: data, //這裡是要傳遞的參數,格式為 data: "{paraName:paraValue}",下面將會看到
// dataType: 'json',
// success: function (result) { //回呼函數,result,傳回值
// alert(result.UserName + result.Mobile + result.Pwd);
// }
// });

$.post("/home/ratearticle", data, RateArticleSuccess, "json");
});
})



//結果顯示

function RateArticleSuccess(result) {
alert(result.UserName + result.Mobile + result.Pwd);
}</span>

後端處理:
複製代碼 代碼如下:
<span style="font-size:14px;">public JsonResult ratearticle(UserInfo model)
{
return Json(model);
}</span>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.