我想获得列表输入名称:Person[0].ID, Person[0].Name,Person[1].ID, Person[1].Name
与:
<input name="Person[0].ID" value= "0" />
<input name="Person[0].Name" value= "Doe" />
<input name="Person[1].ID" value= "1" />
<input name="Person[1].Name" value= "Smith" />
并解析json:
{"Person":[{"ID":"0", "Name":"Doe"}, {"ID":"1", "Name":"Smith"}]}
当我提交表格时,我收到了人员名单,我想要解析json发布ajax。
答案 0 :(得分:1)
您可以创建一个以您想要的特定格式生成JSON的函数,然后将其用于AJAX(或其他)。它可能是这样的:
function generateJsonForPeople() {
var json = {};
json.Person = [];
var x = 0;
while ($('[name="Person[' + x + '].ID"]').length) {
json.Person[x] = {};
json.Person[x].ID = $('[name="Person[' + x + '].ID"]').val();
json.Person[x].Name = $('[name="Person[' + x + '].Name"]').val();
x++;
}
return json;
}
您可以在jsfiddle上看到它。
答案 1 :(得分:0)
我相信你正在寻找serializeArray()
。它不会为您提供确切的结构,但它会在您发送给它的资源上为您提供Person
数组。
我试试:
$.post(url, $('#your-form').serializeArray());
并在资源中发送它以进行转储以查看数据的样子并使其成为您实际需要的格式,如果您确实需要的话。您可以在网络面板中查看转储(或者根本不要执行$ .post,并让表单自然地转到网址。
答案 2 :(得分:0)
这是我的网格:
<table class="Grid" cellspacing="0" cellspacing="0" >
<thead>
<tr>
<th class="headRow headColumn" >STT</th>
<th class="headRow headColumn" >Mã Ngành</th>
<th class="headRow headColumn" colspan="7">Ngành nghề</th>
</tr>
</thead>
<tbody data-bind='foreach: gifts' id="viewModel">
<tr >
<td class="editorTable" colspan="8">
<input type="text" class="editorCombobox" data-bind='value: MaNganh, attr: { name: "ListofNganh[" + $index() + "].MaNganh", id: "ThemMaNganh_"+$index() }' />
</td>
</tr>
<tr class="row">
<td class="rowItemSmall" ><label data-bind='text: $index() + 1'></label> </td>
<td class="rowItemMedium"> @Html.Label("Chi tiết:")</td>
<td class="rowItemLarge" > <input type="text" class="editorTable" data-bind='value: ChiTiet, attr: { name: "ListofNganh[" + $index() + "].ChiTiet" }' />
</td>
<td class="rowItemMedium">@Html.Label("Mã ngành CPC:") </td>
<td class="rowItem">
<input type="text" class="editorTable" data-bind='value: MaNganhCPC, attr: { name: "ListofNganh[" + $index() + "].MaNganhCPC" }' />
</td>
<td class="rowItemSmall"><a href='#' data-bind='click: $root.removeGift'>Xóa</a></td>
<td class="rowItemSmall"><a href='#' data-bind='click: $root.addGift'>Thêm</a></td>
</tr>
@*<tr >
<td></td>
<td>
<button class="btn_blue" data-bind='click: $root.addGift'>Thêm Ngành</button>
</td>
</tr>*@
</tbody>
</table>
这是我的剧本:
<script type="text/javascript" src="/Scripts/knockout-2.2.1.js"></script>
<script type="text/javascript">
var GiftModel = function (gifts) {
var self = this;
self.gifts = ko.observableArray(gifts);
self.addGift = function () {
self.gifts.push({
MaNganh: "",
ChiTiet: ""
, MaNganhCPC: ""
, DienGiai :""
});
};
self.removeGift = function (gift) {
if (self.gifts().length > 1)
self.gifts.remove(gift);
else alert("Phải bắt buộc chọn 1 ngành nghề");
};
};
var viewModel = new GiftModel(@Html.Raw(ViewBag.jsonData));
ko.applyBindings(viewModel, document.getElementById("viewModel"));</script>
这是我的Ajax帖子:
serviceURL = "/N_HOSO_TN_QUAMANG/Update_TabsChiNhanhXML";
jdata = JSON.stringify({
"t": {
"SoBNhan": $('#SoBNhan').val(),
"SoDKKD": $('#SoDKKD').val(),
"TenChiNhanh": $('#TenChiNhanh').val(),
"TenGiaoDichCN": $('#TenGiaoDichCN').val(),
"TenVietTatCN": $('#TenVietTatCN').val(),
"TenTiengViet_DK": $('#TenTiengViet_DK').val(),
"SoNha_DK": $('#SoNha_DK').val(),
"MsQuan_DK": $('#MsQuan_DK').val(),
"MsPhuong_DK": $('#MsPhuong_DK').val(),
"DienThoai_DK": $('#DienThoai_DK').val(),
"Fax_DK": $('#Fax_DK').val(),
"EmailDK": $('#EmailDK').val(),
"WebsiteDK": $('#WebsiteDK').val(),
"ListofNganh": ko.toJS(viewModel).gifts
}
});
$.ajax({
type: "POST",
url: serviceURL,
contentType: 'application/json; charset=utf-8',
data: jdata,
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
var jdataParse = $.parseJSON(data);
//var _SoBNhan = $('#SoBNhan').val();
//if (_SoBNhan == '')
$('#SoBNhan').val(jdataParse.SoBNhan);
}
function errorFunc() {
//alert('error');
}
当我尝试解析JSON时对象jdata:
"{"t":{"SoBNhan":"2014104000057","SoDKKD":"411032000017","TenChiNhanh":"321321","TenGiaoDichCN":"321312","TenVietTatCN":"","SoNha_DK":"321321","MsQuan_DK":"D007","MsPhuong_DK":"G088","DienThoai_DK":"","Fax_DK":"","EmailDK":"","WebsiteDK":"","ListofNganh":[{"DienGiai":"dien giai 01","id":"50111","ChiTiet":"demo","MaNganh":"50111","MaNganhCPC":null,"SoBnhan":"20070123000970"},{"DienGiai":null,"id":"51200","ChiTiet":"demo02","MaNganh":"51200","MaNganhCPC":"demo","SoBnhan":"20070123000970"},{"MaNganh":"46694","ChiTiet":"321321","MaNganhCPC":"3213123","DienGiai":""}]}}"
发布接收jdata我想要的东西。 我的解决方案已经解决,感谢Bill Criswell和Monty82 !!!