1
 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
    }

I am trying to get the values of check-boxes which are checked and store them in array which are then sent to controller through json function. alert("I am done with the split") is not being shown . Please help me.

5
  • 1
    use console log instead of alert Commented Sep 19, 2014 at 12:56
  • 1
    can you show the html? Commented Sep 19, 2014 at 12:57
  • 7
    checkedValues is already an array. What are you trying to split? Commented Sep 19, 2014 at 12:59
  • did you know that the .map function of jquery returns an array? api.jquery.com/jquery.map Commented Sep 19, 2014 at 13:13
  • @onetrickpony - Then why the method in the controller taking null as a parameter for more than one selected check values ? I am having a int[] chkd parameter in the function . It shows null Commented Sep 20, 2014 at 4:50

3 Answers 3

1

Have a look at .serializeArray():

var checkedValues = $('.required:checked').serializeArray();

It returns an array ready for JSON, like so:

[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  },
  {
    name: "d",
    value: "4"
  },
  {
    name: "e",
    value: "5"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

0

split works on string .you can use split on each array index not on whole array.

if you really want to split array checkedValues then loop through this array then use split on each index.

like this:

for( var i=0; i< checkedValues.length;i++){

var arr = checkedValues[i].split(',');
// use arr for requrement.

}

1 Comment

you should rather include in your answer what needs to be changed to achieve what the OP is trying
0

Try

// 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/

2 Comments

console.log is not showing any thing when I check the check-boxes and then run
Not certain what issue is , at each check of checkbox , array of values: true array of values length: 1 , array of values: true,true array of values length: 2 , etc. logged to console at jsfiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.