javascript - 如何在 jQuery 中解析表单数据以使用 JSON?

标签 javascript jquery json forms

 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 。未显示警报(“我已完成拆分”)。请帮助我。

最佳答案

看看.serializeArray() :

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/

相关文章:

ruby-on-rails-3 - 在 Rails 中遍历 JSON

javascript - Azure Web应用程序在引用不存在的路径时抛出错误

jquery - 物化 CSS : Getting accordian to work when set on top of parallax

javascript - 将 AngularJs javascript 文件包含到 Angular cli 中

javascript - 访问函数内部定义的全局变量

jquery - IE7 中的占位符

php - 在 PHP 7.0 中 fatal error : Uncaught Error: Call to undefined function json_encode()

json - 如何在 Haxe 中将 JSON/动态结构转换为 map ?

用于验证 URL 的 Javascript 代码在 IE 和 Chrome 中有效,但在 Firefox 中无效

Javascript:拆分逗号分隔的引号字符串