摘要: js封裝from表單數(shù)據(jù)為json串進(jìn)行ajax提交
json封裝代碼
function getFormJson(frm) { //frm:form表單的id var o = {}; var a = $("#"+frm).serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [ o[this.name] ]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }
返回的數(shù)據(jù)格式為標(biāo)準(zhǔn)的json格式,ajax使用如下:
$.ajax({ type: 'post', url: 'your url', data: getFormJson(frm), success: function(data) { // your code }});
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選