【问题标题】:How to parse form data to work with JSON in jQuery?如何解析表单数据以在 jQuery 中使用 JSON?
【发布时间】:2014-09-19 12:54:58
【问题描述】:
 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 函数发送到控制器。 alert("I'm done with the split") 未显示。请帮帮我。

【问题讨论】:

  • 使用控制台日志代替警报
  • 你能显示html吗?
  • checkedValues 已经是一个数组。你想分裂什么?
  • 你知道jquery的.map函数返回一个数组吗? api.jquery.com/jquery.map
  • @onetrickpony - 那么为什么控制器中的方法将 null 作为多个选定检查值的参数?我在函数中有一个 int[] chkd 参数。它显示为空

标签: javascript jquery json forms


【解决方案1】:

看看.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"
  }
]

【讨论】:

    【解决方案2】:

    split 适用于字符串。您可以使用 split on each array index 而不是 whole array

    如果你真的想拆分数组checkedValues,然后循环遍历这个数组然后use split on each index

    像这样:

    for( var i=0; i< checkedValues.length;i++){
    
    var arr = checkedValues[i].split(',');
    // use arr for requrement.
    
    }
    

    【讨论】:

    • 您应该在回答中包含需要更改的内容以实现 OP 正在尝试的内容
    【解决方案3】:

    试试

    // array of values
    var arr = [];
    $(".required")
        .on("change", function (e) {
        // `checked` `.required` elements
        var checkedValues = $(this).prop("checked");
        // `if` `checkedValues` (any `required` elements `checked`) , continue
        if (checkedValues) {
            // populate `arr` with `.required` `checked` values
            arr.push(checkedValues);
            // do stuff with `arr`
            // `console.log` values in `arr` , `arr` length
            console.log("array of values: " + arr
                        , "array of values length: " + arr.length);
        };
    });
    

    jsfiddle http://jsfiddle.net/guest271314/d332nfkh/

    【讨论】:

    • 当我选中复选框然后运行时,console.log 没有显示任何内容
    猜你喜欢
    • 2012-02-15
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多