var checkedValues = $('.required:checked').map(function () {
return this.value;
}).get();
var arr = new Array(10);
alert(checkedValues);
alert("number of values in it " +checkedValues.length);
if (checkedValues.length > 1) {
alert("I am inside if ");
arr = checkedValues.split(',');
alert("I am done with the split ");
}
else {
arr = checkedValues;
alert("No split needed ");
}
$.getJSON('@Url.Action("testcata", "home")' + + '?Type=' + "@(Session["typ"])" + "&city=" + "@(Session["cit"])" + "&chkd=" + checkedValues,
function (data) {
//code
})
In controller :
public ActionResult testcata(string Type, int? city, int[] chkd)
{
//code
}
我试图获取已选中的复选框的值并将它们存储在数组中,然后通过 json 函数发送到 Controller 。未显示警报(“我已完成拆分”)。请帮助我。
最佳答案
var checkedValues = $('.required:checked').serializeArray();
它返回一个准备好 JSON 的数组,如下所示:
[
{
name: "a",
value: "1"
},
{
name: "b",
value: "2"
},
{
name: "c",
value: "3"
},
{
name: "d",
value: "4"
},
{
name: "e",
value: "5"
}
]
关于javascript - 如何在 jQuery 中解析表单数据以使用 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25934429/