1

normally, we have a ajax request like:

$.ajax({
    type: 'GET',
    url: "/biboundsoptimization",
    data: {
        objects: '2',
    },
    success: function (data) {
        console.log(data);
        alert(data);
    },
    error: function (data, status, er) {
        alert("error: " + data + " status: " + status + " er:" + er);
    }
});

but now I cannot know the number of variables in the data field . For example, when the client enters 2 the data field should be like:

data: {
    objects1: value of objects1,
    objects2: value of objects2,
},

if the client enters 3 the data field should be like:

data: {
    objects1: value of objects1,
    objects2: value of objects2,
    objects3: value of objects3,
},

Before the ajax request, the client has already entered the value of each objects into different input fields. But i don't know the number until he enters it. So i cannot write as above. it is obvious that a loop is required to get all the objects by looping over the number of loops provided by the client. I tried to something like:

for (int i = 1; i < clientValue; i++) {
    'objects' + i: $('#objects' + i).val(),
}

But it doesn't work. Grammatically it is wrong. Can anyone help me with that? Thanks!

2
  • what error are you getting? Do you have a valid value in clientValue? Commented Aug 28, 2013 at 5:24
  • 1
    Why not put all the values into an array, and then pass the array to your ajax call? Commented Aug 28, 2013 at 5:24

5 Answers 5

3

try using the $.each jquery function

var data = {};
$('#objects input').each(function(){
    data.push(this.value);
});
Sign up to request clarification or add additional context in comments.

2 Comments

It does not create an object, which is not what OP asked about.
This worked great for me, but I had to create an empty array like var data = [];.
2

If your data is coming from a FORM.. you can use the jquery serialize function to automatically wrap it all up together for you.

       var formData = $('#yourform').serialize();

you can then do some thing like this in your ajax request

       var request = $.ajax({
            type: 'POST',
            url: 'ajaxHandler.php',
            cache: false,
            data: formData, //all the data is automatically ready for you now
            dataType: 'json'
        });

Comments

1

You can dynamically create the object containing all objects like this:

var dataToPost = {};

for(int i = 1; i < clientValue; i++){
     dataToPost['objects' + i] = $('#objects' + i).val();
}

And then pass in the ajax call like this

data: dataToPost,

Comments

0
var objects = [];

for(int i = 1; i < clientValue; i++){
       objects[i]: $('#objects' + i).val(),
}

Comments

0

add class too all your input and use $.each function

<input id="object1" class="object" />
<input id="object2" class="object" />
var data = {};
var id;
$('.object').each(function(){
   id = $(this).attr('id');
   data[id] = $(this).val();
});

Comments

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.